State Management
Choosing between Context, useReducer, Redux, and Zustand for global state.
What you'll learn
- The spectrum of state management solutions
- When local state, Context, or a library is appropriate
- Redux Toolkit for complex applications
- Zustand for lightweight global state
Concept
The State Management Spectrum
State management in React ranges from simple to complex. Local component state (useState) handles state that only one component needs. Lifted state (state in a common parent) handles state shared by a few siblings. Context handles low-frequency global state like theme and auth. useReducer with Context handles medium-complexity global state. Dedicated libraries (Redux, Zustand, Jotai, Recoil) handle complex, high-frequency, or deeply interconnected state. The key is to use the simplest solution that works — do not reach for Redux on day one.
When to Use What
Use useState for independent local state. Use lifted state when 2-3 sibling components need to share state. Use Context for app-wide, low-frequency values (theme, locale, current user, auth status). Use useReducer + Context when you have complex state transitions that need to be centralized and testable. Use a dedicated library when you have high-frequency updates that would cause too many re-renders with Context, when you need time-travel debugging, or when state is deeply interconnected across many components.
Redux Toolkit
Redux Toolkit (RTK) is the modern, recommended way to use Redux. It dramatically reduces boilerplate with createSlice (which auto-generates action creators and reducers), configureStore (which sets up the store with sensible defaults), and createAsyncThunk for async logic. RTK Query (included in RTK) handles data fetching and caching, replacing the need for a separate data library. Use Redux when you have complex state logic, need middleware, or want the Redux DevTools for time-travel debugging.
Zustand
Zustand is a lightweight state management library that is increasingly popular as a Redux alternative. You create a store with a hook function, and components subscribe to slices of the store. It has minimal boilerplate, supports middleware (persist, devtools), and does not wrap your app in a provider. Zustand is ideal for medium to large apps that need global state without Redux's ceremony. Its selective subscription model also avoids the re-render problems of Context.
Example
// Zustand — lightweight global state with minimal boilerplate.
import { create } from "zustand";
interface BearStore {
bears: number;
increase: () => void;
reset: () => void;
}
const useBearStore = create<BearStore>((set) => ({
bears: 0,
increase: () => set(state => ({ bears: state.bears + 1 })),
reset: () => set({ bears: 0 }),
}));
// Components subscribe to only what they need — selective re-renders.
function BearCounter() {
const bears = useBearStore(state => state.bears);
return <h1>{bears} bears</h1>;
}
function Controls() {
const increase = useBearStore(state => state.increase);
const reset = useBearStore(state => state.reset);
return (
<>
<button onClick={increase}>Add bear</button>
<button onClick={reset}>Reset</button>
</>
);
}
// No provider wrapper needed — just use the hook anywhere.
function App() {
return <><BearCounter /><Controls /></>;
}
Zustand creates a store with create(). Components subscribe to specific slices, so only components using the changed slice re-render. No provider wrapper is needed.
Try it
- JSON Formatter
Inspect store state shapes when designing your state architecture.
- JSON to YAML
Convert state configs to YAML for configuration or documentation.
Common mistakes
The mistake
Reaching for Redux on every project regardless of complexity
The fix
Start with useState and Context. Only add Redux or Zustand when you have complex, high-frequency, or deeply interconnected state that Context cannot handle efficiently. Premature state libraries add unnecessary complexity.
The mistake
Putting everything in global state
The fix
Keep state as local as possible. Only lift state to a common parent or global store when multiple components need it. Over-globalizing state makes components harder to test and reason about in isolation.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.