Reference
useStore
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
useSyncExternalStoreWithSelectorfor 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
undefinedatoms for conditional store usage - Uses reference equality by default - provide a custom compare function for object comparisons