Overview
createReactStore is a wrapper around Zustand’s createStore that provides a React hook interface for accessing store state. It creates a bound hook that can be used directly in React components with optional selectors.
Import
Signature
Parameters
A function that receives This parameter can be omitted to use curried syntax.
set, get, and store parameters and returns the initial state.The initializer function signature:Return Value
A hook function combined with store API methods. Can be called in two ways:
- Without selector:
const state = useStore()- Returns the entire state - With selector:
const value = useStore(state => state.value)- Returns selected value
StoreApi methods:getState(): Get current statesetState(): Update statesubscribe(): Subscribe to state changesgetInitialState(): Get initial state
Types
Examples
Basic Counter Store
Using Entire State
Curried Syntax
Accessing Store API Methods
Complex Store with Middleware
Best Practices
- Use selectors: Always use selectors to avoid unnecessary re-renders
- Keep actions in the store: Define actions as methods in the store state
- Derive state when possible: Compute derived values in selectors rather than storing them
- Use TypeScript: Type your state for better developer experience
- Split large stores: Consider splitting very large stores into smaller, focused stores