Overview
ThecreateStore function creates a new store with reactive state management. It returns an object containing the store’s state access methods, actions, and React hooks.
Signature
Parameters
The initial state of the store. Can include:
- Regular values (primitives, objects, arrays)
- Computed properties using getters
- Synchronizers (e.g.,
storage()for persistence)
Optional function to create custom actions. Receives an object with:
getState: Function to access current stateactions: Object containing all generated setter actionsreset: Function to reset state values
Return Value
Returns the current state of the store.
Object containing all setter actions (generated and custom). Generated actions follow the pattern
set[PropertyName] (e.g., setCount, setName).Each setter accepts either a new value or an updater function:Resets store state to initial values. If no keys provided, resets entire store. Updates are batched automatically.
Subscribes to state changes. The callback runs immediately and on each change to accessed properties. Returns a dispose function to unsubscribe.
Batches multiple state updates into a single notification to subscribers.
React hook that returns state and actions. Automatically subscribes to only the state properties accessed in the component. See useStore.
React hook for subscribing to state changes with lifecycle management. See useStoreEffect.
React hook to hydrate store state once on component mount. See useHydrateState.
Low-level subscription API. Returns a function that accepts a listener and returns an unsubscribe function.
Examples
Basic Store
With Computed Properties
With Storage Persistence
With Custom Actions
Using Effects
Batch Updates
Type Definitions
Notes
- Readonly properties (computed getters) do not have setter actions generated
- State updates that result in equal values (shallow comparison) do not trigger notifications
- Custom actions are automatically wrapped with
batchUpdates - The store integrates with React DevTools via
__stan-js__global