Hooks unstable
React hooks for working with Effect atoms from components. The hooks read,
write, mount, refresh, and subscribe to atoms from RegistryContext, handle
AsyncResult atoms with React Suspense, and expose helpers for reading and
deriving AtomRef values.
Hooks
Subscribes to a writable atom and returns its current value together with a setter for updating it.
When to use
Use when a React component needs both to render the current value of a writable atom and update it from the same component.
Gotchas
In promise and promiseExit modes, the setter observes the atom's next
settled result, not a result correlated with that write. Overlapping writes
can resolve with another write's result. Serialize writes when each caller
needs its own result.
See
- useAtomValue for subscribing to an atom without a setter
- useAtomSet for updating a writable atom without subscribing to its value
Signature
declare function useAtom<R, W, Mode extends "value" | "promise" | "promiseExit" = never>(atom: Writable<R, W>, options?: { readonly mode?: [R] extends [AsyncResult<any, any>] ? Mode : "value";}): readonly [R, "promise" extends Mode ? (value: W) => Promise<Success<R>> : "promiseExit" extends Mode ? (value: W) => Promise<Exit<Success<R>, Failure<R>>> : (value: W | (value: R) => W) => void]useAtomInitialValues
unstableSeeds initial atom values in the current React atom registry.
When to use
Use to seed atom values from a React component after the current registry already exists.
Gotchas
Each atom is initialized at most once for a given registry by this hook, so later calls for the same atom in that registry are ignored.
Signature
declare function useAtomInitialValues(initialValues: Iterable<readonly [Atom<any>, any]>): voiduseAtomMount
unstableMounts an atom in the current React registry for the lifetime of the component.
When to use
Use to keep an atom mounted from a React component without reading, writing, or refreshing it.
Details
The hook uses the current RegistryContext and releases the mount through
React effect cleanup when the component unmounts or when the registry or atom
dependency changes.
See
- useAtomSet for mounting a writable atom while returning a setter
- useAtomRefresh for mounting an atom while returning a refresh callback
Signature
declare function useAtomMount<A>(atom: Atom<A>): voiduseAtomRef
unstableSubscribes to an atom ref and returns its latest value.
When to use
Use when a React component should render from an AtomRef.ReadonlyRef
directly instead of reading an atom through the current registry.
Details
The hook subscribes with ref.subscribe, triggers re-renders through React
state, and returns the current ref.value.
See
- useAtomValue for reading an
Atomfrom the current registry - useAtomRefPropValue for reading a property ref value
Signature
declare function useAtomRef<A>(ref: ReadonlyRef<A>): AuseAtomRefProp
unstableReturns a memoized atom ref for a property of another atom ref.
When to use
Use to derive an AtomRef for one property of an object-shaped atom ref.
Details
The hook memoizes ref.prop(prop) for the [ref, prop] dependency pair and
returns the property ref so callers can read, set, update, or subscribe to
that nested property.
See
- useAtomRef for subscribing to an atom ref value
- useAtomRefPropValue for subscribing directly to a property value
Signature
declare function useAtomRefProp<A, K extends string | number | symbol>(ref: AtomRef<A>, prop: K): AtomRef<A[K]>useAtomRefPropValue
unstableSubscribes to a property ref derived from an atom ref and returns its current value.
When to use
Use when a React component needs only the current value of one property from
an object-shaped AtomRef.
Details
The hook composes useAtomRefProp(ref, prop) with useAtomRef, so the
property ref is memoized for the [ref, prop] pair and then subscribed
through ref.subscribe.
See
- useAtomRefProp for returning the property ref directly
- useAtomRef for subscribing to a whole atom ref value
Signature
declare function useAtomRefPropValue<A, K extends string | number | symbol>(ref: AtomRef<A>, prop: K): A[K]useAtomRefresh
unstableMounts an atom and returns a callback that refreshes it in the current React registry.
When to use
Use to expose a React callback that requests a refresh for an atom without reading or writing its value.
Details
The hook uses the current RegistryContext, mounts the atom for the
component lifetime, and returns a callback that calls registry.refresh.
See
- useAtomMount for mounting an atom without returning a refresh callback
Signature
declare function useAtomRefresh<A>(atom: Atom<A>): () => voiduseAtomSet
unstableMounts a writable atom and returns a setter without subscribing to its value.
When to use
Use when a React component needs to update a writable atom without rendering from that atom's value.
Details
The hook mounts the atom and returns a setter. In value mode the setter
accepts a write value or updater function; for AsyncResult atoms, promise
and promiseExit modes return a promise for the success value or full Exit.
Gotchas
In promise and promiseExit modes, the setter observes the atom's next
settled result, not a result correlated with that write. Overlapping writes
can resolve with another write's result. Serialize writes when each caller
needs its own result.
See
- useAtom for reading and updating the same writable atom
Signature
declare function useAtomSet<R, W, Mode extends "value" | "promise" | "promiseExit" = never>(atom: Writable<R, W>, options?: { readonly mode?: [R] extends [AsyncResult<any, any>] ? Mode : "value";}): "promise" extends Mode ? (value: W) => Promise<Success<R>> : "promiseExit" extends Mode ? (value: W) => Promise<Exit<Success<R>, Failure<R>>> : (value: W | (value: R) => W) => voiduseAtomSubscribe
unstableSubscribes a callback to an atom in the current React registry for the component lifetime.
When to use
Use when a React component needs to run a callback for atom changes without reading the atom value during render.
Details
The subscription is installed in a React effect and cleaned up on unmount or
dependency change. When options.immediate is enabled, the callback receives
the current value when the effect subscribes.
See
- useAtomValue for reading an atom value during render instead of running a callback
Signature
declare function useAtomSubscribe<A>(atom: Atom<A>, f: (_: A) => void, options?: { readonly immediate?: boolean;}): voiduseAtomSuspense
unstableReads an AsyncResult atom through React Suspense, suspending while the
result is initial or configured as waiting.
When to use
Use when a React component should render only after an AsyncResult atom has
left its initial state, with loading delegated to a Suspense boundary.
Details
suspendOnWaiting defaults to false. When includeFailure is true, a
failure result is returned instead of being thrown.
Gotchas
Without includeFailure, failure results are thrown with
Cause.squash(result.cause), so callers need an error boundary for failures.
See
- useAtomValue for reading the raw
AsyncResultvalue without Suspense
Signature
declare function useAtomSuspense<A, E, IncludeFailure extends boolean = false>(atom: Atom<AsyncResult<A, E>>, options?: { readonly includeFailure?: IncludeFailure; readonly suspendOnWaiting?: boolean;}): Success<A, E> | IncludeFailure extends true ? Failure<A, E> : neveruseAtomValue
unstableSubscribes to an atom in the current React registry and returns its current value, optionally mapped through a selector.
When to use
Use when a React component needs to render from an atom value without also returning a setter.
Details
When a selector is provided, the hook subscribes to the atom and applies the
selector to its value from the current RegistryContext.
Gotchas
Late Suspense boundaries hydrate with the same atom snapshot as earlier readers, then update to the live value. The snapshot lasts until all readers unsubscribe. Changes before the first reader hydrates or after the last unsubscribes can still cause mismatches, as can a derived atom first read inside a late boundary.
See
- useAtom for reading and updating a writable atom from one component
- useAtomRef for reading an
AtomRefdirectly
Signature
declare const useAtomValue: { <A>(atom: Atom<A>): A; <A, B>(atom: Atom<A>, f: (_: A) => B): B;}