🌐 Web Development

Zustand

Zustand

Minimal React state management. A Redux alternative with minimal boilerplate.

Detailed Explanation

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.

Code Example

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 ) );

Practical Conversation Example

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!"

Cautions

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.

Related Terms

Learn More