Managing state in React is easy when your application is small.
You can use useState for local state and Context API to share data between components.
But as your application grows, things become more complicated.
Imagine an e-commerce application where multiple components need access to:
User authentication
Shopping cart
Wishlist
Product filters
Notifications
Theme preferences
Passing data through multiple components creates prop drilling, while managing frequently changing shared state with Context API requires additional optimization.
This is where Zustand comes into the picture.
Zustand is a lightweight state management library for React that makes managing global state simple, flexible, and efficient.
Let’s understand why we need Zustand, what it offers, how it works internally, and how it compares with Redux.
❓WHY Do We Need Zustand?
Before understanding Zustand, let’s look at a common React problem.
Problem: Prop Drilling
Imagine this component structure:
App
├── Header
│ └── CartIcon
├── ProductList
│ └── ProductCard
└── CartPageWhen a user adds a product to the cart:
ProductCard needs to update the cart.
CartIcon needs to display the cart count.
CartPage needs to display cart details.
Without shared state management, you may end up passing cart data and update functions through multiple components.
This is called Prop Drilling.
🏪 How Zustand Solves This
Zustand provides a centralized store that any component can access directly.
Instead of passing props between components, they can subscribe to the same store.
Key benefits:
Eliminates unnecessary prop drilling.
Simplifies shared state management.
Supports selective component subscriptions.
Reduces state management boilerplate.
Allows state access outside React components.
The goal is not to replace useState or Context API everywhere. It is to provide a simpler approach when multiple components need shared, frequently changing state.
🎯 WHAT Is Zustand?
Zustand is a small and flexible state management library that allows developers to create centralized stores for managing application state.
It follows a simple approach:
Store + State + Actions + Subscriptions
Its key features include:
Zustand is designed to make global state management straightforward without requiring a large amount of boilerplate.
⚡ HOW Does Zustand Work Internally?
Understanding the internal working of Zustand helps us use it more effectively.
At a high level, Zustand maintains a store outside the React component tree.
The store contains:
State: Application data.
Actions: Functions that update state.
Subscriptions: Listeners that receive store updates.
Internal Workflow
For React components, Zustand uses React’s external-store subscription mechanism, useSyncExternalStore.
When an action updates the store:
Zustand calculates the next state.
The store notifies its subscribers.
React checks the selected state.
Components whose selected values changed are updated.
Important: Zustand uses Object.is by default to compare selected values. For multiple values, useShallow can help avoid unnecessary updates caused by newly created objects or arrays.
This selective subscription mechanism is one of Zustand’s major advantages.
🛠️ HOW to Implement Zustand?
Let’s implement Zustand using a real-world shopping cart example.
Step 1: Install Zustand
npm install zustandStep 2: Create a Store
Create a file named cartStore.js.
import { create } from "zustand";
const useCartStore = create((set) => ({
items: [],
addToCart: (product) =>
set((state) => {
const existingItem = state.items.find(
(item) => item.id === product.id
);
if (existingItem) {
return {
items: state.items.map((item) =>
item.id === product.id
? { ...item, quantity: item.quantity + 1 }
: item
),
};
}
return {
items: [...state.items, { ...product, quantity: 1 }],
};
}),
removeFromCart: (productId) =>
set((state) => ({
items: state.items.filter(
(item) => item.id !== productId
),
})),
clearCart: () => set({ items: [] }),
}));
export default useCartStore;Let’s understand the implementation:
create()creates a Zustand store.set()updates the store.itemsmaintains the cart products.addToCart()adds a product or increases its quantity.removeFromCart()removes a product.clearCart()empties the cart.
Step 3: Add Products to the Cart
import useCartStore from "./cartStore";
function ProductCard({ product }) {
const addToCart = useCartStore(
(state) => state.addToCart
);
return (
<div>
<h3>{product.name}</h3>
<p>${product.price}</p>
<button onClick={() => addToCart(product)}>
Add to Cart
</button>
</div>
);
}Step 4: Display Cart Count
import useCartStore from "./cartStore";
function CartIcon() {
const cartCount = useCartStore((state) =>
state.items.reduce(
(total, item) => total + item.quantity,
0
)
);
return <h3>Cart ({cartCount})</h3>;
}What Happened Here?
Notice that ProductCard updates the cart while CartIcon reads the cart count.
Both components access the same store directly.
We don’t need to pass cart information through intermediate components.
One store. Multiple components. Shared state. No prop drilling.
🧩 Managing Multiple Stores
As applications grow, keeping unrelated state in one massive store can make maintenance difficult.
Zustand allows us to create multiple independent stores.
For example:
src/
├── stores/
│ ├── authStore.js
│ ├── cartStore.js
│ ├── userStore.js
│ ├── notificationStore.js
│ └── themeStore.js
│
├── components/
├── pages/
└── App.jsxEach store manages a specific responsibility.
Best practices:
Organize stores by business responsibility.
Keep related state and actions together.
Avoid creating too many unnecessary stores.
Use selectors to subscribe only to required data.
Consider the slices pattern when a larger store needs modular organization.
Remember, multiple stores are an architectural choice, not a requirement.
⚖️ Zustand vs Redux Toolkit
Both Zustand and Redux Toolkit help manage global state in React applications, but they follow different approaches.
👉 When Should You Choose Zustand?
Choose Zustand when:
You want lightweight global state management.
Your application needs shared client-side state.
You prefer minimal boilerplate.
You want flexible store organization.
You want selective subscriptions without additional setup.
👉 When Should You Choose Redux Toolkit?
Choose Redux Toolkit when:
Your application has complex state transitions.
Your team needs standardized state management patterns.
You need predictable and explicit action/reducer workflows.
You benefit from extensive middleware and ecosystem integrations.
Your application uses RTK Query for API data management.
Important: Redux Toolkit is not outdated. Zustand and Redux Toolkit are both actively useful solutions. The right choice depends on your application’s architecture and requirements.
⭐ Best Practices
Zustand is simple to learn, but building maintainable applications still requires good architectural decisions.
Use Selectors
Avoid subscribing to the entire store when you only need one property.
const user = useUserStore((state) => state.user);Keep Local State Local
Don’t put every component’s state into Zustand.
Use useState for component-specific state.
Separate Responsibilities
Organize stores logically instead of creating one massive global store.
Use Middleware Carefully
Use persist, devtools, and immer when they provide real value.
Avoid Duplicating Server State
Use TanStack Query for API caching, background refetching, and server-state synchronization. Use Zustand for client-side application state.
Avoid Persisting Sensitive Data
Don’t blindly store authentication tokens or sensitive user information in localStorage.
Use TypeScript
Define store interfaces to improve maintainability and catch errors earlier.
🚫 Drawbacks and Limitations of Zustand
No state management library is perfect. Zustand also has some trade-offs.
❓ Is Zustand Suitable for Every Application?
No.
For small and medium-sized applications, Zustand can provide a very clean state management experience.
For larger enterprise applications with strict architectural conventions, complex workflows, and multiple development teams, Redux Toolkit may be a better fit.
However, Zustand can also scale when stores are organized carefully and proper engineering conventions are established.
🏆 Conclusion
State management is an important part of building scalable React applications.
React provides useState and Context API for sharing and managing state, while Redux Toolkit offers a structured approach to complex state management.
Zustand provides another option: a lightweight, flexible, and straightforward way to manage shared application state.
In this article, we explored:
WHY Zustand is needed.
WHAT Zustand is and what problems it solves.
HOW Zustand works internally.
HOW to implement a real-world shopping cart.
HOW to organize multiple stores.
Zustand vs Redux Toolkit.
Best practices and limitations.
The biggest takeaway?
Don’t choose a state management library just because it is popular. Choose it because it solves your application’s problems without introducing unnecessary complexity.
Zustand is a great choice when you want simplicity, flexibility, and effective global client-state management.
What’s Your Take?
Are you using Zustand or Redux Toolkit in your React applications?
Which one do you prefer, and what has your experience been?
Share your thoughts in the comments!






