Overview
TanStack Store is a lightweight reactive store (signals-like) that powers the internals of TanStack libraries. It provides Store for state, Derived for computed values, Effect for side effects, and batch for atomic updates. Framework adapters provide reactive hooks.
Core: @tanstack/store
React: @tanstack/react-store
Installation
Store
Creating a Store
Updating State
Subscribing to Changes
Store Options
Store Properties
Derived (Computed Values)
Derived with Previous Value
Chaining Derived
Effect (Side Effects)
Effect with Cleanup
Batch
Group multiple updates into one notification:
React Integration
useStore Hook
shallow Equality Function
Prevents re-renders when selector returns structurally-equal objects:
Mounting Derived/Effect in React
Module-Level Store Pattern
Framework Adapters
Best Practices
- Define stores at module level - they're singletons
- Use selectors in
useStoreto prevent unnecessary re-renders - Use
shallowwhen selectors return objects/arrays - Always call
mount()on Derived and Effect instances - Always clean up unmount functions (especially in React useEffect)
- Never mutate state directly - always use
setState - Use
batchfor multiple related updates - Use Derived chains for data transformations (filter -> sort -> paginate)
- Return cleanup functions from Effect
fnfor timers/listeners - Select primitives when possible (no equality fn needed)
Common Pitfalls
- Forgetting to
mount()Derived/Effect (they won't activate) - Not cleaning up subscriptions/unmount functions (memory leaks)
- Mutating
store.statedirectly instead of usingsetState - Creating new object references in selectors without
shallow - Using
useStorewithout a selector (subscribes to everything) - Forgetting
eager: truewhen Effect should run immediately


