State Management Approaches in React
Modern React applications use multiple state management approaches depending on scalability, performance, complexity, and data requirements. Understanding when to use Context API, Redux Toolkit, Zustand, or React Query is one of the most important frontend engineering interview topics.
Understanding State Management
State management is the process of storing, updating, and sharing data across components in an application. In small applications, local component state using useState is usually enough. However, as applications grow, problems like prop drilling, duplicated state, difficult debugging, and unnecessary rerenders start appearing.
- Local state works well for isolated UI interactions.
- Global state becomes important when multiple distant components need the same data.
- Modern React applications usually combine multiple state management solutions together.
Example
import { useState } from "react";
export default function Counter() {
const [count, setCount] = useState(0);
return (
<button onClick={() => setCount(count + 1)}>
{count}
</button>
);
}
Context API
Context API is React's built-in solution for sharing state globally without prop drilling. It works well for themes, authentication state, language settings, and lightweight shared UI state. However, Context is not optimized for high-frequency updates because changing context values can rerender many components.
Real World Usage
Context API is commonly used for authentication providers, dark mode themes, localization, and small global UI states.
Pros
- Built into React
- Removes prop drilling
- Good for auth and themes
- Simple API
Cons
- Can cause unnecessary rerenders
- Not ideal for large-scale apps
- Difficult performance optimization
Example
import { createContext, useContext } from "react";
const ThemeContext = createContext("dark");
export default function App() {
return (
<ThemeContext.Provider value="dark">
<Dashboard />
</ThemeContext.Provider>
);
}
function Dashboard() {
const theme = useContext(ThemeContext);
return <div>{theme}</div>;
}
Interview Tips
- Context API is not a replacement for Redux.
- Mention rerender problems during interviews.
- Talk about provider wrapping patterns.
Redux Toolkit
Redux Toolkit is the modern official way to use Redux. It simplifies Redux development by removing boilerplate and providing built-in utilities for slices, reducers, async actions, and immutable updates. Redux is highly scalable and excellent for large enterprise applications.
Real World Usage
Redux Toolkit is widely used in enterprise dashboards, fintech platforms, ecommerce systems, and large applications requiring predictable state architecture.
Pros
- Centralized predictable state
- Excellent DevTools
- Scales very well
- Powerful middleware ecosystem
Cons
- More setup than Zustand
- Can become over-engineered
- Extra complexity for small apps
Example
import { createSlice } from "@reduxjs/toolkit";
const counterSlice = createSlice({
name: "counter",
initialState: {
value: 0,
},
reducers: {
increment: (state) => {
state.value += 1;
},
},
});
export const { increment } = counterSlice.actions;
export default counterSlice.reducer;
Interview Tips
- Always mention Redux Toolkit instead of old Redux.
- Explain centralized state architecture.
- Mention Redux DevTools advantage.
Zustand
Zustand is a lightweight modern state management library focused on simplicity and performance. It provides a minimal API with less boilerplate than Redux and supports selector-based subscriptions for optimized rerenders.
Real World Usage
Zustand is commonly used in startups, SaaS dashboards, portfolio projects, and medium-scale React applications.
Pros
- Minimal boilerplate
- Very easy to learn
- Good performance
- Simple global stores
Cons
- Smaller ecosystem
- Less strict architecture
- Can become messy in huge apps
Example
import { create } from "zustand";
type CounterStore = {
count: number;
increment: () => void;
};
export const useCounterStore = create<CounterStore>((set) => ({
count: 0,
increment: () =>
set((state) => ({
count: state.count + 1,
})),
}));
Interview Tips
- Mention low boilerplate advantage.
- Compare Zustand with Redux Toolkit.
- Discuss simplicity vs scalability tradeoff.
React Query / TanStack Query
React Query is designed specifically for server state management. Instead of manually storing API data inside Redux or Context, React Query handles fetching, caching, retries, synchronization, background updates, and loading states automatically.
Real World Usage
React Query is heavily used in production-grade applications that communicate with APIs frequently, especially SaaS products and dashboards.
Pros
- Excellent caching
- Automatic refetching
- Built-in loading and error handling
- Reduces backend requests
Cons
- Only solves server state
- Not meant for UI state
- Requires understanding cache invalidation
Example
import { useQuery } from "@tanstack/react-query";
export default function UsersPage() {
const { data, isLoading } = useQuery({
queryKey: ["users"],
queryFn: async () => {
const res = await fetch("/api/users");
return res.json();
},
});
if (isLoading) {
return <p>Loading...</p>;
}
return (
<div>
{data.map((user) => (
<p key={user.id}>{user.name}</p>
))}
</div>
);
}
Interview Tips
- Explain difference between server state and client state.
- Mention caching and invalidation.
- Discuss why API data should not always go into Redux.
Comparison
Every state management solution solves different problems. Choosing the correct approach depends on application size, complexity, server communication, performance requirements, and developer experience.
| Feature | Context API | Redux Toolkit | Zustand | React Query |
|---|---|---|---|---|
| Primary Purpose | Simple global UI state | Large application state | Lightweight global state | Server state management |
| Boilerplate | Very Low | Medium | Very Low | Low |
| Performance | Can rerender heavily | Excellent | Very Good | Excellent |
| Best Use Case | Themes/Auth | Enterprise Apps | Medium Apps | API Data |
| Caching Support | No | Manual | Manual | Built-in |
| Learning Curve | Easy | Medium | Easy | Medium |
| DevTools | Basic | Excellent | Good | Excellent |
| Scalability | Limited | Excellent | Good | Excellent for APIs |