ReactMedium

Context vs Redux?

Context is React's built-in way to pass a value down the tree; it does not manage state or optimize updates, and every consumer re-renders when the value changes. Redux (today, Redux Toolkit) is a state management library with a central store, reducers, middleware, selective subscriptions and DevTools. Use Context for low-frequency shared values and Redux for large, frequently changing, complex app state.

Comparison

Context (+ useState/useReducer)Redux Toolkit
Installbuilt in@reduxjs/toolkit, react-redux
Purposedependency injection of a valuepredictable global state management
Re-rendersall consumers on any value changeonly components whose selected slice changed
Middleware, side effectsnone built inthunks, listener middleware, RTK Query
DevToolsReact DevTools onlyRedux DevTools with time-travel and action log
Boilerplatelowmoderate (much less than classic Redux)
Server data cachingnoRTK Query

When Context is enough

Theme, locale, auth user, feature flags, small apps, and component-library internals.

When Redux shines

  • Large teams wanting strict, consistent patterns.
  • Complex state transitions with many actions.
  • Need for action history, debugging and time-travel.
  • Existing Redux codebases.

Other options in between

Zustand and Jotai offer store-like selective subscriptions with very little boilerplate. For server data, TanStack Query or RTK Query rather than either Context or plain Redux.

Interview answer

"They solve different problems; Context transports values, Redux manages state. I pick based on update frequency, app size and team needs."

Code example

tsx
1// Redux Toolkit store with selective subscriptions
2import { configureStore, createSlice, type PayloadAction } from "@reduxjs/toolkit";
3import { Provider, useDispatch, useSelector } from "react-redux";
4
5const cartSlice = createSlice({
6 name: "cart",
7 initialState: { items: [] as { id: string; qty: number }[] },
8 reducers: {
9 added(state, action: PayloadAction<string>) {
10 const item = state.items.find((i) => i.id === action.payload);
11 item ? item.qty++ : state.items.push({ id: action.payload, qty: 1 }); // Immer makes this immutable
12 },
13 },
14});
15
16const store = configureStore({ reducer: { cart: cartSlice.reducer } });
17type RootState = ReturnType<typeof store.getState>;
18
19function CartCount() {
20 // Re-renders only when the count changes, not on every store update
21 const count = useSelector((s: RootState) => s.cart.items.reduce((n, i) => n + i.qty, 0));
22 return <span>{count}</span>;
23}
24
25function AddButton({ id }: { id: string }) {
26 const dispatch = useDispatch();
27 return <button onClick={() => dispatch(cartSlice.actions.added(id))}>Add</button>;
28}
29
30export const App = () => <Provider store={store}><CartCount /><AddButton id="pen" /></Provider>;

Practice this question

Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the React set.

Open in study app