Skip to main content

Reference

useStore

A React hook that subscribes to a TanStack Store atom and returns the selected state. The component will re-render when the selected state changes according to the comparison function.
AnyAtom | undefined
The store atom to subscribe to. Can be undefined to support conditional usage.
(snapshot: TSnapshot) => T
A function that selects a slice of state from the atom’s snapshot. Use this to optimize re-renders by selecting only the data your component needs.
(a: T, b: T) => boolean
default:"Object.is"
Optional comparison function to determine if the selected state has changed. Defaults to reference equality (Object.is). Use shallow for shallow object comparison.

Returns

T
The selected state value from the atom.

Usage

Basic Usage

With Custom Comparison

Selecting Multiple Values

Notes

  • Built on top of React’s useSyncExternalStoreWithSelector for optimal concurrent rendering support
  • Automatically handles subscription cleanup when the component unmounts
  • The selector function should be stable or memoized to avoid unnecessary re-subscriptions
  • Supports undefined atoms for conditional store usage
  • Uses reference equality by default - provide a custom compare function for object comparisons