Zustand is a state management library for React applications that has received a lot of attention in recent years. It has been praised for being as robust as Redux, yet simple to use like the Context API. In this article, we will explain the basic usage of Zustand and why it is so popular.
What is Zustand?
Zustand (German for “state”) is a small, fast, and scalable state management library for React. It is characterized by very little boilerplate (clichéd description) and provides an intuitive Hooks-based API.
Main benefits of Zustand
- Simple API: Unlike Redux, there is no need to configure Actions, Reducers, Providers, etc., making your code cleaner.
- Rendering optimization: Components can subscribe to only the states they need, which prevents unnecessary re-rendering.
- Provider not required: Since React’s Context API is not used, there is no need to surround the component tree with a provider.
Basic usage
Zustand is very simple to use. First, just create a Store and call it from your component.
1. Create a Store
Use the create function to define the state and the action that updates it.
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 }),
}))
2. Use in components
Call the created Store (custom hook) within the component. Optimize performance by retrieving only the state you need.
function Counter() {
// 必要なステートとアクションのみを取得
const count = useStore((state) => state.count)
const increment = useStore((state) => state.increment)
return (
<div>
<p>Count: {count}</p>
<button onClick={increment}>Increment</button>
</div>
)
}
Asynchronous processing made easy
Zustand allows you to write asynchronous processing as is without introducing special middleware (such as Redux Thunk).
const useUserStore = create((set) => ({
users: [],
loading: false,
fetchUsers: async () => {
set({ loading: true })
const response = await fetch('/api/users')
const data = await response.json()
set({ users: data, loading: false })
},
}))
Summary
Due to its simplicity and high performance, Zustand is increasingly being adopted not only for small to medium-sized projects but also for large-scale projects. The learning cost is also very low, so if you’re starting a new React project, consider Zustand as your state management option.

