Live Demo
Try it on StackBlitz
Open the interactive demo in StackBlitz
Store Setup
Create a store with a counter state. Stan.js automatically generates setter functions for all state properties.store.ts
The
createStore function returns:useStore- React hook to access state and actionsreset- Function to reset specific state keys to their initial valuesgetState- Function to get current state outside of Reactactions- Object containing all auto-generated setter functions
Component Implementation
Let’s break down the counter into separate display and control components to demonstrate Stan.js’s efficient subscription model.- Complete Example
- Display Component
- Controls Component
App.tsx
Key Concepts
Auto-Generated Actions
Stan.js automatically creates setter functions for every property in your store:Reset Functionality
Reset any state property back to its initial value:Accessing State Outside React
UsegetState() to read current state values outside of React components:
Performance Benefits
Minimal Re-renders
Minimal Re-renders
Components only re-render when the specific state properties they access change. In our example:
CounterDisplayre-renders only whencounterchangesCounterControlsnever re-renders (setters don’t change)
No Provider Needed
No Provider Needed
Unlike Context API or other state management solutions, Stan.js doesn’t require wrapping your app in a Provider. Just import and use.
TypeScript Inference
TypeScript Inference
Full TypeScript support with automatic type inference. Your IDE will autocomplete state properties and setter functions.
Next Steps
Todo List
Learn about arrays, persistence, and complex state
Async Data
Handle async operations and loading states
API Reference
Explore all createStore options
Computed Values
Create derived state with getters