React · Lesson 13 of 15 · 4 min
State Management Libraries (Redux Toolkit & Alternatives)
This is about articulating the trade-offs, not just knowing one library's API.
After this, you will be able to explain when to use a global state manager, why Redux Toolkit is the standard, and how to choose between it and lighter alternatives like Zustand or Context. This is a core topic for any mid-to-senior role.
When Do You Actually Need a Global State Manager?
You need one when passing state through multiple layers of props ("prop drilling") becomes unmanageable, or when many non-related components need to access or modify the same state.
The first tool you should consider is React's built-in Context API, often paired with useReducer for more complex state logic. It's dependency-free and works well for low-frequency updates like theme changes or user authentication status.
The trap: useContext has a performance pitfall. When the context value changes, every component that consumes that context re-renders, even if it only cares about a small, unchanged part of the state. This is why dedicated libraries exist. They provide optimized subscriptions so components only re-render when the specific data they need changes.
How to Explain Redux Toolkit
Redux is a pattern for managing application state in a predictable, centralized container called a "store". In an interview, explain that its value comes from making state changes explicit and traceable. Redux was historically known for excessive boilerplate. Redux Toolkit (RTK) is the modern, official solution that solves this.
RTK's main features are:
configureStore: Sets up a store with sensible defaults, including the Redux DevTools extension.createSlice: Generates reducers and action creators from a simple object, drastically cutting boilerplate. It uses Immer internally, which lets you write "mutating" code that it safely converts into immutable updates.
The data flow is unidirectional and is the key to its predictability.
You can describe this flow simply: A component dispatches an action. The store runs the corresponding reducer, which computes the new state. Components subscribed to that state then re-render.
Here is a minimal, complete example of a counter. This is all you need to demonstrate the core concepts.
import { configureStore, createSlice, PayloadAction } from '@reduxjs/toolkit';
import { Provider, useSelector, useDispatch } from 'react-redux';
import React from 'react';
// 1. Define a slice of state
const counterSlice = createSlice({
name: 'counter',
initialState: { value: 0 },
reducers: {
increment: (state) => {
state.value += 1; // Immer lets us "mutate" state here
},
decrement: (state) => {
state.value -= 1;
},
},
});
const { increment, decrement } = counterSlice.actions;
// 2. Create the Redux store
const store = configureStore({
reducer: {
counter: counterSlice.reducer,
},
});
// 3. Create a component that uses the state
function Counter() {
const count = useSelector((state: ReturnType<typeof store.getState>) => state.counter.value);
const dispatch = useDispatch();
return (
<div>
<h2>Count: {count}</h2>
<button onClick={() => dispatch(increment())}>Increment</button>
<button onClick={() => dispatch(decrement())}>Decrement</button>
</div>
);
}
// 4. Provide the store to the app
export default function App() {
return (
<Provider store={store}>
<Counter />
</Provider>
);
}
Distinguishing Client State from Server State
This is the most important distinction to make in a modern state management discussion. Failing to separate these concepts is a red flag.
Client State is data that lives exclusively in the browser and is not persisted on a server. Think UI state: is a modal open, what is the current theme, what are the contents of a form? Regular RTK slices are excellent for this.
Server State is data that originates from an API. It's a cache of data that lives on a server. This includes user profiles, product lists, etc. It has a complex lifecycle involving fetching, caching, re-fetching, and invalidation.
The trap: Never use a standard Redux slice (or
useState/useReducer) to manage server state. You will inevitably end up manually implementing loading states, error handling, caching, and re-fetching logic. This is a solved problem.
For server state, use a dedicated data-fetching library. The two main choices are RTK Query (part of Redux Toolkit) and TanStack Query (formerly React Query). They provide hooks that handle all the complexity of server state for you.
When an interviewer asks "How would you fetch and display a list of products?", the right answer is not "I'd dispatch a thunk and store it in a Redux slice." The senior answer is: "I'd use a tool like RTK Query or TanStack Query. It handles caching, background refetches, and provides simple hooks like useGetProductsQuery that return the data, loading, and error states, which simplifies the component."
How to Compare Redux with Lighter Alternatives
Your ability to articulate trade-offs is more important than deep knowledge of any single library. Frame your answer around complexity and specific needs.
-
Context API +
useReducer: Use it for simple, global state that doesn't update frequently. It's built-in. Its main drawback is performance, as all consumers re-render on any change. -
Zustand: A popular lightweight alternative. It's minimalist and uses a simple hook-based API. You can subscribe to parts of the state without the boilerplate of Redux selectors. It's a great choice when you need a simple global store without the ceremony of Redux.
-
Jotai / Recoil: These are "atomic" state managers. State is broken down into tiny, independent pieces (atoms). Components subscribe only to the atoms they need. This provides the most granular optimization, preventing unnecessary re-renders. Use them when performance is critical and you have many small, independent pieces of state.
Your default choice doesn't have to be Redux. A good thought process is: start with local state (useState). If state needs to be shared, lift it. If lifting causes prop drilling, use Context. If Context causes performance issues, or state logic becomes complex, evaluate Zustand for simplicity or Redux Toolkit for a more structured, feature-rich solution.
Try it
Take a component from one of your projects that uses useState and passes props down two or more levels. Refactor it to use a simple Zustand store for the shared state. It should take less than 20 minutes and demonstrate the reduction in prop drilling.
Next: Testing React Components with Jest & React Testing Library
Lock it in — apply this lesson under interview conditions.