Details

Language / Topic
javascriptJavaScript
Category
Libraries
Compatible Frameworks
react
nextjs
remix
Source
pmndrs

Rules

balanced
- Create stores with `create()`: define state and actions in a single function. Access with hooks.
- Keep stores small and focused — one store per domain concern, not one global store.
- Use selectors (`useStore(state => state.field)`) to subscribe to specific slices — avoids unnecessary re-renders on unrelated state changes.
- Use selectors to subscribe to specific state slices: `useStore((state) => state.count)` — avoid re-renders from unrelated state changes.
- Use `immer` middleware for immutable updates on deeply nested state.
- Define actions inside the store alongside state — colocate state and the logic that modifies it.
- Use `persist` middleware for localStorage/sessionStorage persistence.