State Management Overview
Redux Toolkit
Used for complex authentication state that needs persistence across sessions
Zustand
Used for simpler, component-level state like auth data and page titles
Redux Toolkit
Redux Toolkit is configured with redux-persist to maintain authentication state across browser sessions.Store Configuration
The Redux store is configured insrc/store/store.tsx:
src/store/store.tsx
Key Configuration Details
Key Configuration Details
Redux Persist Configuration:
key: "root"- The key used in localStoragestorage- Uses localStorage by default- Ignored actions prevent serialization warnings for persist actions
RootState- Type for the entire Redux state treeAppDispatch- Type for the dispatch function with thunk support
Auth Slice
The authentication slice manages user authentication state:src/store/auth/authSlice.tsx
State Properties
State Properties
- status: Authentication status (
'checking'|'not-authenticated'|'authenticated') - fullname: User’s full name (null when not authenticated)
- permissions: Array of permission slugs for the authenticated user
Thunks (Async Actions)
Async operations are handled using thunks insrc/store/auth/thunks.tsx:
src/store/auth/thunks.tsx
Thunk Pattern
Thunk Pattern
Thunks allow you to:
- Perform async operations (API calls)
- Dispatch multiple actions
- Access the current state
- Handle errors gracefully
startGetUserInfo thunk:- Fetches user data from the API
- Extracts permissions from user roles
- Dispatches the
loginaction on success - Dispatches
logouton error
Using Redux in Components
- With Hooks
- With Thunks
Zustand
Zustand provides a simpler, more lightweight alternative for local state management.Auth Store
The Zustand auth store (src/store/authStore.ts) provides an alternative authentication state management:
src/store/authStore.ts
Zustand Store Pattern
Zustand Store Pattern
Zustand stores are:
- Simple: No boilerplate like Redux reducers and actions
- TypeScript-friendly: Full type inference
- Hook-based: Used directly as React hooks
- Performant: Component only re-renders when used state changes
- State properties (
user,permissions,isAuthenticated) - Actions as methods (
setAuth,clearAuth)
Page Title Store
A simpler example showing Zustand for UI state:src/store/pageTitleStore.ts
Using Zustand in Components
- Reading State
- Updating State
- Multiple Values
Redux vs Zustand: When to Use Which?
- Use Redux When
- Use Zustand When
- State needs to persist across sessions (with redux-persist)
- You need middleware for logging, analytics, etc.
- Complex state transformations with multiple reducers
- DevTools integration for time-travel debugging
- Team is already familiar with Redux patterns
Current Implementation
In the MKing Admin app, the current state management is in transition: The router primarily uses Zustand:src/router/AppRouter.tsx (excerpt)
Best Practices
Selector Optimization
Use selector functions to derive data and prevent unnecessary re-renders.
Action Naming
Use clear, descriptive action names:
setAuth, clearAuth, login, logout.Type Safety
Always define TypeScript interfaces for your state and actions.
Immutability
Redux Toolkit uses Immer for immutable updates. Zustand requires manual immutability.
Next Steps
Project Structure
Understand the overall project organization
Routing
Learn about the routing system and protected routes