React State Management
Comprehensive guide to modern React state management patterns, from local component state to global stores and server state synchronization.
When to Use This Skill
- Setting up global state management in a React app
- Choosing between Redux Toolkit, Zustand, or Jotai
- Managing server state with React Query or SWR
- Implementing optimistic updates
- Debugging state-related issues
- Migrating from legacy Redux to modern patterns
Core Concepts
1. State Categories
| Type |
Description |
Solutions |
| Local State |
Component-specific, UI state |
useState, useReducer |
| Global State |
Shared across components |
Redux Toolkit, Zustand, Jotai |
| Server State |
Remote data, caching |
React Query, SWR, RTK Query |
| URL State |
Route parameters, search |
React Router, nuqs |
| Form State |
Input values, validation |
React Hook Form, Formik |
2. Selection Criteria
Small app, simple state → Zustand or Jotai
Large app, complex state → Redux Toolkit
Heavy server interaction → React Query + light client state
Atomic/granular updates → Jotai
Quick Start
Zustand (Simplest)
// store/useStore.ts
import { create } from 'zustand'
import { devtools, persist } from 'zustand/middleware'
interface AppState {
user: User | null
theme: 'light' | 'dark'
setUser: (user: User | null) => void
toggleTheme: () => void
}
export const useStore = create<AppState>()(
devtools(
persist(
(set) => ({
user: null,
theme: 'light',
setUser: (user) => set({ user }),
toggleTheme: () => set((state) => ({
theme: state.theme === 'light' ? 'dark' : 'light'
})),
}),
{ name: 'app-storage' }
)
)
)
// Usage in component
function Header() {
const { user, theme, toggleTheme } = useStore()
return (
<header className={theme}>
{user?.name}
<button Theme</button>
</header>
)
}
Detailed patterns and worked examples
Detailed pattern documentation lives in references/details.md. Read that file when the navigation tier above is insufficient.
Best Practices
Do's
- Colocate state - Keep state as close to where it's used as possible
- Use selectors - Prevent unnecessary re-renders with selective subscriptions
- Normalize data - Flatten nested structures for easier updates
- Type everything - Full TypeScript coverage prevents runtime errors
- Separate concerns - Server state (React Query) vs client state (Zustand)
Don'ts
- Don't over-globalize - Not everything needs to be in global state
- Don't duplicate server state - Let React Query manage it
- Don't mutate directly - Always use immutable updates
- Don't store derived data - Compute it instead
- Don't mix paradigms - Pick one primary solution per category
Migration Guides
From Legacy Redux to RTK
// Before (legacy Redux)
const ADD_TODO = "ADD_TODO";
const addTodo = (text) => ({ type: ADD_TODO, payload: text });
function todosReducer(state = [], action) {
switch (action.type) {
case ADD_TODO:
return [...state, { text: action.payload, completed: false }];
default:
return state;
}
}
// After (Redux Toolkit)
const todosSlice = createSlice({
name: "todos",
initialState: [],
reducers: {
addTodo: (state, action: PayloadAction<string>) => {
// Immer allows "mutations"
state.push({ text: action.payload, completed: false });
},
},
});
1---2name: react-state-management3description: Master modern React state management with Redux Toolkit, Zustand, Jotai, and React Query. Use when setting up global state, managing server state, or choosing between state management solutions.4metadata:5 source: https://skillmd.com/skills/ericgrill/react-state-management6---78# React State Management910Comprehensive guide to modern React state management patterns, from local component state to global stores and server state synchronization.1112## When to Use This Skill1314- Setting up global state management in a React app15- Choosing between Redux Toolkit, Zustand, or Jotai16- Managing server state with React Query or SWR17- Implementing optimistic updates18- Debugging state-related issues19- Migrating from legacy Redux to modern patterns2021## Core Concepts2223### 1. State Categories2425| Type | Description | Solutions |26| ---------------- | ---------------------------- | ----------------------------- |27| **Local State** | Component-specific, UI state | useState, useReducer |28| **Global State** | Shared across components | Redux Toolkit, Zustand, Jotai |29| **Server State** | Remote data, caching | React Query, SWR, RTK Query |30| **URL State** | Route parameters, search | React Router, nuqs |31| **Form State** | Input values, validation | React Hook Form, Formik |3233### 2. Selection Criteria3435```36Small app, simple state → Zustand or Jotai37Large app, complex state → Redux Toolkit38Heavy server interaction → React Query + light client state39Atomic/granular updates → Jotai40```4142## Quick Start4344### Zustand (Simplest)4546```typescript47// store/useStore.ts48import { create } from 'zustand'49import { devtools, persist } from 'zustand/middleware'5051interface AppState {52 user: User | null53 theme: 'light' | 'dark'54 setUser: (user: User | null) => void55 toggleTheme: () => void56}5758export const useStore = create<AppState>()(59 devtools(60 persist(61 (set) => ({62 user: null,63 theme: 'light',64 setUser: (user) => set({ user }),65 toggleTheme: () => set((state) => ({66 theme: state.theme === 'light' ? 'dark' : 'light'67 })),68 }),69 { name: 'app-storage' }70 )71 )72)7374// Usage in component75function Header() {76 const { user, theme, toggleTheme } = useStore()77 return (78 <header className={theme}>79 {user?.name}80 <button onClick={toggleTheme}>Toggle Theme</button>81 </header>82 )83}84```8586## Detailed patterns and worked examples8788Detailed pattern documentation lives in `references/details.md`. Read that file when the navigation tier above is insufficient.8990## Best Practices9192### Do's9394- **Colocate state** - Keep state as close to where it's used as possible95- **Use selectors** - Prevent unnecessary re-renders with selective subscriptions96- **Normalize data** - Flatten nested structures for easier updates97- **Type everything** - Full TypeScript coverage prevents runtime errors98- **Separate concerns** - Server state (React Query) vs client state (Zustand)99100### Don'ts101102- **Don't over-globalize** - Not everything needs to be in global state103- **Don't duplicate server state** - Let React Query manage it104- **Don't mutate directly** - Always use immutable updates105- **Don't store derived data** - Compute it instead106- **Don't mix paradigms** - Pick one primary solution per category107108## Migration Guides109110### From Legacy Redux to RTK111112```typescript113// Before (legacy Redux)114const ADD_TODO = "ADD_TODO";115const addTodo = (text) => ({ type: ADD_TODO, payload: text });116function todosReducer(state = [], action) {117 switch (action.type) {118 case ADD_TODO:119 return [...state, { text: action.payload, completed: false }];120 default:121 return state;122 }123}124125// After (Redux Toolkit)126const todosSlice = createSlice({127 name: "todos",128 initialState: [],129 reducers: {130 addTodo: (state, action: PayloadAction<string>) => {131 // Immer allows "mutations"132 state.push({ text: action.payload, completed: false });133 },134 },135});136```