What is a Store?
A store in Stan.js is a centralized container for your application state. It provides a simple, type-safe way to manage state that can be accessed and updated from anywhere in your application.Stan.js stores are built on vanilla JavaScript and can be used with or without React. The React integration uses
useSyncExternalStore under the hood for optimal performance.Creating a Store
Stores are created using thecreateStore function. The simplest store requires just an initial state object:
Return Values
ThecreateStore function returns several utilities:
| Property | Type | Description |
|---|---|---|
useStore | Hook | React hook to access and subscribe to store state |
getState | Function | Get current state snapshot (non-reactive) |
actions | Object | Auto-generated setter functions for each state property |
reset | Function | Reset state values to their initial values |
effect | Function | Subscribe to state changes outside of React |
batchUpdates | Function | Batch multiple state updates into a single notification |
Auto-Generated Actions
Stan.js automatically generates setter functions for each state property. The naming convention isset + capitalized property name:
Functional Updates
Actions support functional updates, similar to React’suseState:
Stan.js uses shallow equality checks to prevent unnecessary re-renders. If the new value is equal to the previous value, subscribers won’t be notified.
Implementation Details
Under the hood, Stan.js stores work through a subscription system:State Initialization
Fromsrc/vanilla/createStore.ts:84-139, the store initializes state by:
- Processing each property in the initial state object
- Handling special values like Synchronizers (for persistence)
- Setting up getter properties for computed values
- Creating listener arrays for each state key
Action Generation
Actions are created at store initialization time (src/vanilla/createStore.ts:20-50):
Readonly Properties
Properties defined with getters are automatically treated as readonly. Stan.js won’t generate setter actions for them:Accessing State
In React Components
Use theuseStore hook to access state reactively:
Outside React
UsegetState() to get a snapshot of the current state:
getState() returns a snapshot and is not reactive. Changes to the store won’t update the returned object.Resetting State
Thereset function restores state values to their initial values:
src/vanilla/createStore.ts:196-205:
Type Safety
Stan.js is built with TypeScript and provides full type inference:Best Practices
Keep it Simple
Start with a simple state structure. You can always refactor later.
Use Descriptive Names
Choose clear property names that describe what the state represents.
Initialize with Types
Always specify types for arrays and objects using
as assertions.Avoid Nested Objects
Keep state flat when possible. Deep nesting can impact performance.
Next Steps
State Management
Learn patterns for managing complex state
Subscriptions
Understand the subscription system
Computed Values
Use getters for derived state
Custom Actions
Create custom actions for complex logic