Overview
ThecreateStore function creates a standalone state management store that works in any JavaScript environment without React. It provides reactive state updates, computed values, subscriptions, and custom actions.
Signature
Parameters
The initial state object for the store. Each property becomes a trackable state value.
- Cannot contain functions as top-level values
- Supports computed properties using getters
- Supports synchronizers for persistence
Optional function to define custom actions that can update multiple state values at once.Receives an object with:
getState: Function to get current stateactions: Auto-generated setter actionsreset: Function to reset state values
Return Value
Function that returns the current state of the store. See getState for details.
Object containing auto-generated setter functions (like
setCount) and any custom actions you defined.Each setter follows the pattern set{PropertyName} and accepts either:- A new value directly
- A function that receives the previous value and returns the new value
Subscribe to state changes with automatic dependency tracking. See effect for details.
Batch multiple state updates to trigger listeners only once. See batchUpdates for details.
Reset specific state values (or all values if no keys provided) back to their initial values.
Low-level subscription function that returns an unsubscribe function. Most use cases should use
effect instead.Basic Example
Computed Values
Use getters to create computed values that automatically update when dependencies change:Custom Actions
Create custom actions for complex state updates:Reset State
Reset values back to their initial state:Type Safety
TypeScript provides full type inference:See Also
- getState - Access current state
- effect - Subscribe to changes
- batchUpdates - Optimize multiple updates