JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

Built for developers preparing for JavaScript, React & TypeScript interviews.

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeQuestionsreact
PrevNext

Learn the concept

State Management

react
mid
state-management

When should you use Context API vs Redux for state management?

context
redux
state-management
zustand
performance
re-renders
Quick Answer

Use Context API for simple, low-frequency state (theme, auth, locale) shared across components. Use Redux (or Redux Toolkit) for complex, frequently-updated state that benefits from middleware, selective subscriptions, time-travel debugging, and predictable update patterns.

Detailed Explanation

Context API — What It Is:

Context is a React feature for passing data through the component tree without prop drilling. It is not a state management solution — it is a dependency injection mechanism. You still need useState or useReducer to actually manage the state; Context just provides a way to share it.

Context API — When It Works Well:

  • Theme (light/dark mode) — changes infrequently
  • Authentication (current user) — changes on login/logout only
  • Locale/Language — changes rarely
  • Feature flags — set once on app load

Context API — The Re-Render Problem:

When a context value changes, every component that calls useContext() for that context re-renders — even if the component only uses a small part of the value. There is no built-in way to subscribe to a slice of context.

JSX
// If ANY field in this object changes, ALL consumers re-render
const AppContext = createContext({ user: null, theme: 'light', cart: [] });

For frequently-changing state (like a shopping cart or real-time data), this causes excessive re-renders.

Redux Toolkit — When It Shines:

Redux Toolkit (RTK) is the modern, official way to use Redux. It provides:

  • Selective subscriptions — useSelector only re-renders a component when its selected slice of state changes
  • Middleware — Intercept actions for logging, analytics, async operations (thunks, sagas)
  • DevTools — Time-travel debugging, action history, state diff inspection
  • Predictable patterns — Actions describe what happened, reducers describe how state changes
  • RTK Query — Built-in data fetching and caching (replaces manual API logic)

Modern Alternatives:

  • Zustand — Lightweight (1KB), hooks-based, selective subscriptions, no boilerplate. Great for moderate complexity.
  • Jotai — Atomic state model, each piece of state is an independent atom. Bottom-up approach vs Redux's top-down.
  • TanStack Query — Not a state manager but handles server state (fetching, caching, synchronization). Eliminates 80% of what people used Redux for.

Decision Framework:

| Scenario | Solution | |----------|----------| | Theme, auth, locale | Context + useState | | Form state, UI toggles | Local useState | | Server data (API responses) | TanStack Query or RTK Query | | Moderate shared client state | Zustand | | Complex client state with middleware needs | Redux Toolkit | | Simple atomic state | Jotai or Zustand |

Code Examples

Context API re-render problemJSX
const AppContext = createContext();

function AppProvider({ children }) {
  const [user, setUser] = useState(null);
  const [theme, setTheme] = useState('light');
  const [notifications, setNotifications] = useState([]);

  // Every time ANY of these values changes, ALL consumers re-render
  const value = { user, setUser, theme, setTheme, notifications, setNotifications };

  return <AppContext.Provider value={value}>{children}</AppContext.Provider>;
}

// This component re-renders when notifications change,
// even though it only uses theme!
function ThemeToggle() {
  const { theme, setTheme } = useContext(AppContext);
  return <button onClick={() => setTheme(t => t === 'light' ? 'dark' : 'light')}>{theme}</button>;
}

// Mitigation: Split into separate contexts
const ThemeContext = createContext();
const UserContext = createContext();
const NotificationContext = createContext();
// Now each consumer only re-renders for its own context changes

Real-World Applications

Use Cases

E-Commerce Shopping Cart

Shopping carts with real-time price calculations benefit from Redux or Zustand's selective subscriptions, as Context would re-render the entire checkout page on every cart change.

Theme and Localization

Theme toggling and language switching are ideal Context use cases — they change infrequently and affect the entire app.

Mini Projects

State Management Comparison

intermediate

Build the same todo app with Context, Redux Toolkit, and Zustand. Add React DevTools profiling to compare re-render counts.

Context Re-Render Counter

beginner

Create a visualization that shows how many components re-render when a Context value changes vs a Redux selector.

Industry Examples

Meta

Created Context API as part of React 16.3 and later contributed to the Recoil atomic state library for more granular state management.

Vercel

Next.js Commerce template uses Zustand for client-side cart state and React Context for theme/auth, demonstrating the complementary pattern.

Resources

React Docs - Passing Data Deeply with Context

docs

Redux Toolkit - Getting Started

docs

Zustand - GitHub

docs

Related Questions

What is Redux and Redux Thunk, and how do they manage application state and async operations?

senior
state-management

What is React Context and when should you use it?

mid
context
Previous
How do React component lifecycle methods map to hooks in functional components?
Next
Build an OTP (One-Time Password) input component in React that auto-advances focus between fields, supports paste, and handles backspace navigation.
PrevNext