Zustand
Zustand
Minimal React state management. A Redux alternative with minimal boilerplate.
Zustand
Minimal React state management. A Redux alternative with minimal boilerplate.
Zustand is a lightweight and flexible state management library for React. It means "state" in German, and it is developed by the pmndrs (Poimandres) team together with Jotai and Valtio.
You can manage global state with a simple API, without Redux's complex boilerplate. Because it is Hooks-based, it is familiar to React developers, and it works even without a Provider.
With its small bundle size (~1KB) and intuitive usage, it has become a popular choice to replace Redux.
Creating a Basic Store
import { create } from 'zustand';
// μ€ν μ΄ μμ±
const useStore = create((set) => ({
count: 0,
increment: () => set((state) => ({ count: state.count + 1 })),
decrement: () => set((state) => ({ count: state.count - 1 })),
reset: () => set({ count: 0 }),
}));
// μ»΄ν¬λνΈμμ μ¬μ©
function Counter() {
const { count, increment, decrement } = useStore();
return (
<div>
<span>{count}</span>
<button onClick={increment}>+</button>
<button onClick={decrement}>-</button>
</div>
);
}
Optimizing Performance with Selective Subscription
const useStore = create((set) => ({
user: { name: 'νκΈΈλ', email: 'hong@example.com' },
posts: [],
setUser: (user) => set({ user }),
}));
// λμ μ: μ 체 μν ꡬλ
- posts λ³κ²½μμλ 리λ λλ§
const { user } = useStore();
// μ’μ μ: νμν κ²λ§ μ ν - user λ³κ²½μμλ§ λ¦¬λ λλ§
const user = useStore((state) => state.user);
// μ¬λ¬ κ° μ ν (shallow λΉκ΅)
import { shallow } from 'zustand/shallow';
const { name, email } = useStore(
(state) => ({ name: state.user.name, email: state.user.email }),
shallow
);
Using Middleware (persist + devtools)
import { create } from 'zustand'; import { persist, devtools } from 'zustand/middleware'; const useAuthStore = create( devtools( persist( (set) => ({ user: null, token: null, login: (user, token) => set({ user, token }), logout: () => set({ user: null, token: null }), }), { name: 'auth-storage' } // localStorage key ), { name: 'AuthStore' } // DevTools name ) );
Team lead
"Have you decided how to handle state management for the new project?"
Developer
"I'm planning to use Zustand instead of Redux. It has less boilerplate and a smaller bundle size. All it takes is a single simple create function to get started."
Team lead
"Does it support DevTools? I need that for debugging."
Developer
"Yes, if you use the devtools middleware, you can see everything in Redux DevTools!"
Selective Subscription Is a Must
If you subscribe to the entire state, components re-render even on unrelated state changes. Use a selector function to select only the state you need.
Use shallow When Comparing Objects
When selecting multiple values as an object, you need to apply shallow comparison to prevent unnecessary re-renders.
Manage Server State Separately
Server state such as API data is better handled by a dedicated library such as TanStack Query. Focus Zustand on client state.