Function Signature
Parameters
T
The initial value for the atom. Creates a mutable atom that can be updated with
set().(prev?: NoInfer<T>) => T
A getter function that computes the atom’s value. Creates a readonly atom that automatically recomputes when dependencies change.
prev- The previous computed value (optional)
AtomOptions<T>
Optional configuration for the atom.
Returns
Atom<T>
Returns a mutable
Atom instance when initialized with a value. Provides:get()- Get the current valueset(value)- Set a new valueset(updater)- Update with a functionsubscribe(observer)- Subscribe to changes
ReadonlyAtom<T>
Returns a readonly
ReadonlyAtom instance when initialized with a function. Provides:get()- Get the current computed valuesubscribe(observer)- Subscribe to changes
Examples
Basic Atom
Computed Atom
Custom Comparison
Subscribing to Atoms
Chaining Computed Atoms
Managing Complex State
Atom with Object State
Using Previous Value in Computed Atoms
When to Use Atoms vs Stores
Use Atoms
- Fine-grained reactivity
- Building custom reactive primitives
- Performance-critical code
- Simple value containers
Use Stores
- Application state management
- Higher-level abstractions
- When you need the
stateproperty - Better TypeScript inference in some cases
Related APIs
- createStore - Higher-level store abstraction
- createAsyncAtom - Atom for async operations
- batch - Batch multiple atom updates
- flush - Manually flush pending updates