Guides
React: keeping components up to date
Built-in fine-grained signals with useLive, or MobX-native observability with one option. Same types either way.
Every entity field, relation, collection, load status and dirty flag is reactive. Reactivity is allocated lazily, per field and per query, so a 100,000-row replica costs nothing until you observe it.
Built-in signals (default)
With no options, the client uses its own fine-grained signals. The examples use the todo app from the quick start. @clientdb/react gives you two ways to subscribe a component:
| Tracks | Use it for | |
|---|---|---|
useLive(fn) | only what fn reads | picking data: lists, counts, lookups |
live(Component) | everything the component reads while rendering | components that render entity fields and relations, such as list rows |
useLive
"use client";
import { useLive } from "@clientdb/react";
import type { Project } from "@/sync/db";
export function OpenTodos({ project }: { project: Project }) {
const titles = useLive(() => {
return project.todos.where({ done: false }).orderBy("updatedAt", "desc").all.map((todo) => todo.title);
});
const count = useLive(() => {
return project.todos.count;
});
return (
<section>
<h2>
{project.name} · {titles.length} of {count} open
</h2>
<ul>
{titles.map((title, index) => {
return <li key={index}>{title}</li>;
})}
</ul>
</section>
);
}useLive(fn) subscribes the component to whatever fn reads and re-renders only when the result changes. There is no dependency array: fn runs on every render, so it always sees the current props, and queries are cached by their filters, so running db.todo.where({ projectId }) again finds the same live query. Expensive derivations belong in db.$computed.
live()
useLive tracks what its function reads, not what the component reads afterwards from the returned entities. Rendering todo.title or todo.author.name from a useLive result in the same component is a read nobody observes: the component keeps showing the first value. Wrap such components in live():
"use client";
import { live, useLive } from "@clientdb/react";
import { db, type Todo } from "@/sync/db";
const TodoRow = live(({ todo }: { todo: Todo }) => {
return (
<li>
{todo.title} · {todo.author?.name ?? "Unknown"} · {todo.done ? "done" : "open"}
</li>
);
});
export function TodoList({ projectId }: { projectId: string }) {
const todos = useLive(() => {
return db.todo.where({ projectId }).orderBy("updatedAt", "desc").all;
});
return (
<ul>
{todos.map((todo) => {
return <TodoRow key={todo.id} todo={todo} />;
})}
</ul>
);
}Todo and Project are the entity types, exported from src/sync/db.ts as in the quick start: type DB = EntitiesOf<Schema>; export type Todo = DB["Todo"];.
live(Component) tracks every entity field, relation and collection read while rendering (like MobX observer) and memoizes the component on its props (shallow comparison, as memo). Only the row whose data changed re-renders. It is safe under StrictMode, supports hooks inside, and works with the MobX adapter too (through DatabaseProvider).
The untracked-read warning
In development, reading an entity field or relation during render outside useLive and live() logs a one-time warning per model.property:
clientdb: project.todos read during render outside useLive()/live(); this component won't update when it changes. Read it inside useLive(() => …), or wrap the component in live() (observer() with MobX).Fix it by moving the read into useLive, or wrapping the component in live().
MobX
Pass the MobX adapter, and everything becomes MobX-observable. Wrap components in observer() and read data directly:
"use client";
import { createDatabaseClient, type EntitiesOf } from "@clientdb/client";
import { mobxReactivity } from "@clientdb/mobx";
import { schema, type Schema } from "./schema";
export const db = createDatabaseClient({ schema, reactivity: mobxReactivity() });
export type DB = EntitiesOf<Schema>;
export type Todo = DB["Todo"];
export type Project = DB["Project"];"use client";
import { observer } from "mobx-react-lite";
import type { Project } from "@/sync/db";
export const OpenTodos = observer(function OpenTodos({ project }: { project: Project }) {
return (
<ul>
{project.todos.where({ done: false }).all.map((todo) => {
return <li key={todo.id}>{todo.title}</li>;
})}
</ul>
);
});Watch out
Don't use useLive inside observer components: with MobX reactivity it throws. The public types are identical with either reactivity.
DatabaseProvider
Wrap your app in DatabaseProvider once. With ready, it renders fallback until the eager models are usable, so components can read them right away (lazy models show their own loading state). It also gives useLive and live() the client's reactivity, MobX included:
"use client";
import { DatabaseProvider } from "@clientdb/react";
import { Spinner } from "@/components/spinner";
import { db } from "@/sync/db";
export function Providers({ children }: { children: React.ReactNode }) {
return (
<DatabaseProvider db={db} ready fallback={<Spinner />}>
{children}
</DatabaseProvider>
);
}Outside React
db.$computed and db.$effect work anywhere, with whichever reactivity the client uses:
import { db } from "@/sync/db";
const titles = db.$computed(() => {
return db.todo.all.map((todo) => {
return todo.title;
});
});
const stop = db.$effect(() => {
console.log(titles.get());
});With a MobX client, MobX's own computed and autorun work too.
Performance notes
- Observed queries are maintained incrementally from the replica's change sets (O(log n) per affected query). Unobserved queries are computed on demand.
- One observed field write, with a filtered collection watching that column, costs about 11–25 µs on 10k–100k rows.
- Client memory is about 177 bytes per row. See Benchmarks.