# Atoms (https://effetch-query.monarcode.dev/docs/react/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](https://effetch-query.monarcode.dev/docs/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](https://effetch-query.monarcode.dev/docs/react#select-part-of-a-value). If
several do, derive an atom with `Atom.map` and read it with `useAtomValue`:

```tsx
import { useAtomValue } from "@effect/atom-react";
import * as AsyncResult from "effect/reactivity/AsyncResult";
import * as Atom from "effect/reactivity/Atom";

import { todos } from "./todos.js";

const openTitles = Atom.map(
  todos({ query: { done: false } }),
  AsyncResult.map((list) => list.map((todo) => todo.title)),
);

export function OpenTitles() {
  const result = useAtomValue(openTitles);
  return <p>{AsyncResult.getOrElse(result, () => []).join(", ")}</p>;
}
```

`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](https://effetch-query.monarcode.dev/docs/queries/freshness#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:

```tsx
import * as AsyncResult from "effect/reactivity/AsyncResult";
import * as Atom from "effect/reactivity/Atom";

import { todos } from "./todos.js";

const openTodos = todos({ query: { done: false } });

const titlesOf = (result: Atom.Type<typeof openTodos>) =>
  AsyncResult.map(result, (list) => list.map((todo) => todo.title));

// The registry, which the server value reads the query's cache from.
const registryAtom = Atom.readable((get) => get.registry);

export const openTitles = Atom.withServerValue(
  Atom.map(openTodos, titlesOf),
  (get) => titlesOf(Atom.getServerValue(openTodos, get(registryAtom))),
);
```

`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](https://effetch-query.monarcode.dev/docs/client#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](https://effetch-query.monarcode.dev/docs/cache).

## What's next

- [Select part of a value](https://effetch-query.monarcode.dev/docs/react#select-part-of-a-value): derive a
  value for one component, with the mount refetches kept.
- [Read and write the cache](https://effetch-query.monarcode.dev/docs/cache): read and write cached values
  from Effect code.
- [The query client](https://effetch-query.monarcode.dev/docs/client): the client's registry, layer and
  lifetime.