Zustand Store
Zustand state management with Slice architecture for the Flux monorepo.
Architecture
- •Factory:
apps/web/src/lib/create-store.ts- thecreateStorewrapper, local to web app - •Store definitions:
apps/web/src/stores/- domain stores live in the consuming app - •Store creation:
createWithEqualityFn+shallowdefault + optionaldevtools+subscribeWithSelector - •Organization: Multi-store by domain, each store uses Slice pattern
- •Selectors: Strict mode - all state access through selector objects
- •Types: Full TypeScript - Store = State & SliceAction1 & SliceAction2 & ...
Quick Reference
| Pattern | Reference |
|---|---|
| Store creation & Slice pattern | store-patterns |
| Selector patterns | selector-patterns |
| React integration | react-integration |
Directory Structure
App-local stores (factory + domain stores):
code
apps/web/src/stores/
└── <domain>/
├── store.ts # store creation + slice composition
├── initial-state.ts # state types + initial values
├── index.ts # public exports
└── slices/
└── <slice>/
├── action.ts
├── initial-state.ts
└── selectors.ts
Rules
- •Never access store state directly in components - always use selectors
- •Every selector function must be exported via a
xxxSelectorsobject - •Use
shallowas default equality fn; useisEqualfor deep objects only when needed - •Prefix internal-only actions with
internal_ - •Keep state flat - avoid deeply nested objects
- •Async actions call services then
set()- no middleware for persistence - •Use
subscribeWithSelectoron stores that need external subscriptions