Reference
useStore
Atom<TState> | ReadonlyAtom<TState>
The store atom to subscribe to.
(state: TState) => TSelected
default:"(d) => d"
Optional function that selects a slice of state from the atom. Defaults to returning the entire state.
UseStoreOptions<TSelected>
Optional configuration object.
Returns
TSelected
The selected state value from the atom.
Usage
Basic Usage
Selecting Multiple Values
Custom Equality Function
Using Entire State
With Shallow Comparison
Selecting Derived Values
Conditional Rendering
Multiple Store Subscriptions
Notes
- Implements a custom
useSyncExternalStoreshim for Preact compatibility - Automatically handles subscription cleanup when the component unmounts
- Defaults to
shallowequality comparison for optimal performance - The selector function runs on each potential update - keep it simple or memoize expensive computations
- Built specifically for Preact - does not require
preact/compat - Uses
useLayoutEffectanduseStateinternally for synchronization - Works with both Preact function components and class components (via function children)