Atoms
Use queries as effect/reactivity atoms.
A query is an atom: a reactive value from Effect's effect/reactivity
that code can read and subscribe to. Anything that works on atoms works on
queries, including @effect/atom-react's hooks. This page shows the
common case, a value derived from a query that several components share,
and what to watch for: mount refetches, server renders and writes.
The examples import todos.ts from Getting started.
Share a derived value
If only one component needs a value derived from a query, use select:
see Select part of a value. If
several do, derive an atom with Atom.map and read it with useAtomValue:
import { } from "@effect/atom-react";
import * as from "effect/reactivity/AsyncResult";
import * as from "effect/reactivity/Atom";
import { } from "./todos.js";
const = .(
({ : { : false } }),
.(() => .(() => .)),
);
export function () {
const = ();
return <>{.(, () => []).(", ")}</>;
}useAtomValue isn't useQuery. Reading the derived atom reads the query,
so it fetches the query if it isn't cached, but it doesn't run
the mount policy: a component that reads a derived atom doesn't refetch the query
when it's stale (refetchOnMount) or failed (retryOnMount). To keep
those, read the query with useQuery somewhere in the same tree.
Derived atoms in a server render
In a server render, useQuery reads only the cache and never starts a
fetch. A derived atom does start one: Atom.map makes a plain atom, and a
server render reads a plain atom as the browser does, by reading the query.
To read the cache instead, give the derived atom a server value with
Atom.withServerValue. Here, in a server render, openTitles reads what
the cache has, as useQuery does:
import * as from "effect/reactivity/AsyncResult";
import * as from "effect/reactivity/Atom";
import { } from "./todos.js";
const = ({ : { : false } });
const = (: .<typeof >) =>
.(, () => .(() => .));
// The registry, which the server value reads the query's cache from.
const = .(() => .);
export const = .(
.(, ),
() => (.(, ())),
);Atom.getServerValue reads a query as a server render does: its cached
value, or a waiting Initial when the cache doesn't have it. Only a
server render, and the browser's first render while it hydrates the
server's HTML, read the server value; after that, the browser reads
openTitles as it reads any atom.
If the server render doesn't need to show the value, wrap the derived atom
in Atom.withServerValueInitial instead. It's the short form of
Atom.withServerValue for a server value of a waiting Initial: the atom
reads that in a server render, cached or not, and fetches nothing.
The registry is the client's
client.registry is the AtomRegistry, the store that holds the
client's cache. QueryProvider gives it to @effect/atom-react, so your
own atoms and atom-react's hooks share it. Don't reset or dispose the
registry: dispose the client, as
Dispose a client shows.
Write with Query.set, not an atom write
You can write to a query like any writable atom, with registry.set or
useAtomSet. The written value shows until a fetch that settles after the
write replaces it. But a write to a query that isn't cached doesn't stop
its first fetch: the value is kept, and the query still fetches when it's
first read. Query.set caches the value, so nothing fetches: see
Read and write the cache.
What's next
- Select part of a value: derive a value for one component, with the mount refetches kept.
- Read and write the cache: read and write cached values from Effect code.
- The query client: the client's registry, layer and lifetime.