Overview
Athena ERP uses a hybrid state management approach, combining:- Zustand for global application state (authentication, preferences)
- React Query for server state (data fetching, caching, mutations)
- React useState for local component state
- react-hook-form for form state
Global State with Zustand
Authentication Store
The primary Zustand store manages authentication state:src/store/authStore.ts
- Persistence: State persists to LocalStorage via
persistmiddleware - Multi-tenancy: Supports school impersonation for superadmins
- Role-based access: Stores user roles for authorization
- Token management: Stores JWT token for API requests
- Memberships: Tracks user memberships across multiple schools
Using the Auth Store
Accessing Store Outside React
Zustand stores can be accessed outside React components:src/api/client.ts
Server State with React Query
Query Client Configuration
src/main.tsx
Query Hooks Pattern
Custom hooks encapsulate data fetching logic:src/hooks/useStudents.ts
Using Query Hooks in Components
Query Key Strategy
Hierarchical query keys for efficient invalidation:API Client Configuration
Axios Instance with Interceptors
src/api/client.ts
- Automatic token injection
- Multi-tenancy via
X-School-Idheader - Global 401 handling (auto-logout)
- Base URL from environment variables
Form State with react-hook-form
Form with Validation
Local Component State
useState for UI State
State Management Best Practices
When to Use Each Tool
| State Type | Tool | Use Case |
|---|---|---|
| Authentication | Zustand | User session, tokens, roles |
| App preferences | Zustand | Theme, language, UI settings |
| Server data | React Query | API data, background sync |
| Form inputs | react-hook-form | Complex forms with validation |
| UI state | useState | Modals, dropdowns, local toggles |
| Derived state | useMemo | Computed values from props/state |