Skip to main content

Reference

useStore

A Preact 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 equality function.
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 useSyncExternalStore shim for Preact compatibility
  • Automatically handles subscription cleanup when the component unmounts
  • Defaults to shallow equality 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 useLayoutEffect and useState internally for synchronization
  • Works with both Preact function components and class components (via function children)