Overview
GAIA uses Zustand for state management across web, desktop, and mobile apps. Zustand provides a lightweight, flexible alternative to Redux with minimal boilerplate and excellent TypeScript support.Why Zustand?
Minimal Boilerplate
No providers, actions, or reducers required
TypeScript First
Full type inference and safety
React Integration
Hooks-based API for React
DevTools
Redux DevTools integration
Basic Store Pattern
A basic Zustand store follows this pattern:Real Store Examples
Chat Store
The chat store manages conversations and messages with IndexedDB integration:- Normalized state: Messages grouped by conversation ID
- Optimistic updates: Show UI changes before server confirmation
- Sorted data: Messages sorted by timestamp for correct display
Todo Store with Optimistic Updates
The todo store demonstrates optimistic updates with rollback:- Optimistic updates: Update UI immediately, rollback on error
- Error handling: Graceful error recovery with user feedback
- Selectors: Export convenience hooks for common selections
- DevTools: Integration with Redux DevTools for debugging
UI State Store
Manage global UI state like sidebars, modals, and themes:- Persistence: Settings persist across sessions
- Simple state: Boolean flags and enums for UI state
Advanced Patterns
Store Slices
Split large stores into logical slices:Computed Values
Derive state with selectors:Async Actions
Handle async operations with loading states:Best Practices
1. Separate State and Actions
2. Use Selectors for Performance
3. Avoid Nested Objects
4. Type Everything
Middleware
Persist
Persist state to localStorage:DevTools
Integrate with Redux DevTools:Testing Stores
Common Patterns Summary
Optimistic Updates
Update UI immediately, rollback on error
Normalized State
Store entities by ID in objects, not arrays
Computed Values
Use selectors to derive state
Async Actions
Handle loading and error states
Next Steps
Component Structure
Learn component organization patterns
Web App
Explore the Next.js application
API Integration
Connect stores to the backend
Testing
Write tests for your stores