Skip to main content

Observer

The Observer<T> type defines the callback interface for observing changes to an atom’s value. It follows the RxJS observer pattern.

Type Definition

Properties

next

Called whenever the atom emits a new value. Type:
Optional: Yes

error

Called when an error occurs during the atom’s operation. Type:
Optional: Yes

complete

Called when the atom completes and will no longer emit values. Type:
Optional: Yes

Usage Examples

Basic observer with next callback

Full observer with all callbacks

Simplified function syntax

The subscribe method also accepts a simpler syntax where you can pass the callbacks as separate function arguments:

With error and complete handlers

Type-safe observer

Conditional observer logic

Observer Pattern

The Observer type follows the standard observer pattern used in reactive programming:
  1. next: Handles the normal flow of data
  2. error: Handles any errors that occur
  3. complete: Handles the completion of the observable stream
In TanStack Store, atoms typically only use the next callback, as atoms are long-lived and don’t usually complete or error in the traditional RxJS sense.
  • Subscription - The object returned when subscribing with an observer
  • Atom - The writable atom type that can be observed
  • ReadonlyAtom - The read-only atom type that can be observed