Supplying a domain
AtemnoDomain builds a domain and supplies it to everything below it. It takes the same options as the ReactiveDomain constructor.
import { AtemnoDomain } from 'atemno/react';
function App() {
return (
<AtemnoDomain onError={error => console.error(error)}>
<Counter />
</AtemnoDomain>
);
}The domain is built once and kept for the life of the component, so inline handlers are fine. The latest handlers are always the ones called.
It is not destroyed on unmount. Unmounting drops every subscription the tree held, the domain releases its nodes in turn, and the domain becomes garbage. Destroying it would break StrictMode, which remounts a component with the same state.
Starting state
initialize receives the new domain once, before anything below renders. Write the starting state there, such as data the server loaded for this request.
<AtemnoDomain initialize={domain => domain.set(user, currentUser)}>
<App />
</AtemnoDomain>AtemnoDomain nests. Hooks use the nearest one, so a subtree can run against its own isolated state.
Reading
import { useAtomState, useComputedValue } from 'atemno/react';
function Counter() {
const [value, setValue] = useAtomState(count);
const doubled = useComputedValue(double);
return (
<button onClick={() => setValue(v => v + 1)}>
{value} doubled is {doubled}
</button>
);
}Reads go through useSyncExternalStore, so server rendering works and concurrent rendering cannot tear. Hooks lists them all.
State and unmounting
A domain releases a node once nothing tracks it. If the only component reading an atom unmounts, the atom's state goes with it. See node lifetime for how to keep it.