Overview
createStore provides a powerful and type-safe way to manage application state. It supports subscriptions, batched updates, middleware plugins, and selective state updates with full TypeScript inference.
Import
Signature
Parameters
A function that receives
set, get, and api and returns the initial state.Optional configuration object.
Custom equality function to determine if state has changed. Defaults to
Object.is.If
true, listeners are notified synchronously. If false (default), updates are batched.Array of plugins to extend store functionality.
Return Value
The store API object with the following methods:
Returns the current state.
Updates the state. Can accept partial state or a function that receives the previous state.
Subscribes to state changes. Returns an unsubscribe function.
Subscribe to a slice of state using a selector function.
Returns the initial state that was set when the store was created.
Resets the state to the initial state.
Returns the set of all active listeners.
Usage Examples
Basic Counter Store
Subscribe to Changes
Async Actions
Custom Equality Function
Fire Listener Immediately
Reset to Initial State
Types
StoreStateInitializer
SetState
SetStateOptions
SubscribeOptions
Advanced Features
Batched Updates
By default, multiplesetState calls within the same execution context are batched:
Synchronous Updates
Force synchronous updates when needed:Notes
- State updates are batched by default for performance
- Partial updates are merged with the current state automatically
- Use
shouldReplace: trueto completely replace the state instead of merging - Subscribers only fire when state actually changes (determined by
equalityFn) - The store is framework-agnostic and can be used with any UI library