v0 · Developer PreviewSorus is under active development. APIs may change between releases.

Hooks

Example

The send button reads $canSend with useValue. Under memo, it rerenders only when that value changes. ChatRoot gives the components under it the chat’s realm.

tsx
import { RealmProvider, useValue } from "@frondruntime/sorus-react";
import { memo, type ReactNode } from "react";
import { app } from "./realm.ts";
import { $canSend } from "./state.ts";

// Under memo, it rerenders when $canSend changes, not with the form around it.
const SendButton = memo(function SendButton() {
  const canSend = useValue($canSend);
  return (
    <button type="submit" disabled={!canSend}>
      Send
    </button>
  );
});

function ChatRoot({ children }: { readonly children: ReactNode }) {
  // useValue needs no provider; useRealm and useSelect below it do.
  return <RealmProvider realm={app}>{children}</RealmProvider>;
}

useSelect runs view logic over observables: the mark beside a message rerenders when that message becomes, or stops being, the one replied to, and not on a keystroke.

tsx
import { useSelect } from "@frondruntime/sorus-react";
import { $composer } from "./state.ts";

// Rerenders when this message becomes, or stops being, the one replied to.
function ReplyMark({ id }: { readonly id: string }) {
  const replying = useSelect((read) => read($composer).replyTo === id);
  return replying ? <span>↩</span> : null;
}

Rules

  • There is no observer wrapper: the hooks read through React’s useSyncExternalStore, so they work with the React Compiler as they are.
  • To rerender less, read in a small component under memo, and pass it values that are the same on every render: an observable, a key, or data made once. An array, object, or function built in the render defeats memo.
  • In an event handler, read inside Realm.untracked and write through a function that calls Realm.update, such as send.
  • A component’s own state is a box made once in the provider’s realm: const realm = useRealm(), then useState(() => Box.make(realm, ...)).
  • A slow render after a write reads useDeferredValue(useValue($x)). A startTransition around the write changes nothing: React renders a change of an outside store as a blocking update.

Answers

Call Answers
useValue($x) The value of $x. Needs no provider.
useValues([$a, $b]) The values as a typed array, which keeps its identity until one changes. Needs no provider; observables of two realms throw.
useSelect((read) => ...) The function’s result, rerendering when it changes under Object.is. Throws without a provider. An inline arrow runs it and a layout effect on every render; useValue of a computed value does neither.
<RealmProvider realm={realm}> Gives the components under it a realm.
useRealm() The nearest provider’s realm. Throws without a provider.

With Effect

A model from sorus-effect is read with the same hooks (Classes).

See also

  • Start: the chat’s composer from box to button.
  • Collections: a list that rerenders one row.
  • Lifecycles: what an observer holds, and a realm in a React effect.
  • Classes: the models a component reads on Effect.
  • Testing: a realm per test.