Overview
SIGEAC uses a hybrid approach to state management, combining different solutions for different types of state:React Query
Server state & data fetching
Zustand
Client-side global state
React Context
Authentication & theme
Server State with React Query
React Query manages all server state, including data fetching, caching, and synchronization.Setup
TheQueryClientProvider wraps the entire application:
app/layout.tsx
providers/query-provider.tsx:6
Query Pattern
Create custom hooks for data fetching:hooks/general/clientes/useGetClients.ts
hooks/general/clientes/useGetClients.ts:1
Using the Query Hook
components/ClientList.tsx
Mutation Pattern
Create custom hooks for data mutations:actions/general/clientes/actions.ts
actions/general/clientes/actions.ts:16
Using the Mutation Hook
components/ClientForm.tsx
Query Key Patterns
- List Queries
- Detail Queries
- Filtered Queries
Use array keys with dependencies:
Cache Invalidation
Invalidate queries after mutations:Optimistic Updates
Update UI before server response:Client State with Zustand
Zustand manages client-side global state that doesn’t belong on the server.Company Store
The main Zustand store manages company and location selection:stores/CompanyStore.ts
stores/CompanyStore.ts:1
Using Zustand Stores
Server-Side Usage
Zustand stores can’t be used in Server Components. Use the helper hook:hooks/helpers/use-store.tsx
hooks/helpers/use-store.tsx:1
Usage:
Context API for Authentication
The AuthContext manages authentication state and user data:contexts/AuthContext.tsx
contexts/AuthContext.tsx:1
Using Authentication
Local State with useState
For component-specific state, use React’s built-inuseState:
State Management Decision Tree
Quick Guide:
- Server data (clients, flights, etc.) → React Query
- Global settings (company, location) → Zustand
- User auth → Auth Context
- Component UI (modals, dropdowns) → useState
- Forms → React Hook Form
Best Practices
Keep state close to where it's used
Keep state close to where it's used
Only lift state to global stores when multiple components need it.
Use proper query keys
Use proper query keys
Include all dependencies in query keys:
Invalidate queries after mutations
Invalidate queries after mutations
Always invalidate related queries:
Use selectors in Zustand
Use selectors in Zustand
Prevent unnecessary re-renders:
Next Steps
Data Fetching
Learn data fetching patterns in detail
Custom Hooks
Create reusable custom hooks
