Skip to main content

Function Signature

Creates a reactive atom, which is the low-level primitive for fine-grained reactivity in TanStack Store. Atoms are similar to stores but with a more minimal API.

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 value
  • set(value) - Set a new value
  • set(updater) - Update with a function
  • subscribe(observer) - Subscribe to changes
ReadonlyAtom<T>
Returns a readonly ReadonlyAtom instance when initialized with a function. Provides:
  • get() - Get the current computed value
  • subscribe(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 state property
  • Better TypeScript inference in some cases