React Client Side State Management with Zustand<!-- --> | <!-- -->Patrick Desjardins Blog
Patrick Desjardins Blog
Patrick Desjardins picture from a conference

React Client Side State Management with Zustand

Posted on: January 27, 2023

In a previous post I explained that the React Context is re-rendering a lot if you change the state inside the context. I concluded that using a state management, like Redux, might still be relevant. However, Redux comes with its baggage of middleware, connection, hooks, actions, etc. So, here enters a more straightforward solution: Zustand.

Why use Zustand for your State Management?

First of all, it is very lean and simple. It took me 20 minutes to grasp what was needed.

Second, the usage of Zustand is very similar to useState, making it easy to understand but also to use.

Third, when you change a portion of your store (object that is deep with properties) only the components interested to a specific part gets updated.

Forth, it works with code outside React. Useful if you have a library that is framework agnostic but wants to push changes or read data.

Finally, fifth, it is type-safe with TypeScript.

When not to use Zustand?

I do not recommend using Zustand for data that is coming and changing from the backend. For example, I rely on Apollo Client for all my data coming from the backend. Not having the data duplicated in two places is a good strategy. If you want a copy in Zustand, you will need many useEffect or code in a callback (onComplete) to update your state in Zustand. While possible, it implies synchronization which is a source of potential error.

How Zustand works in a Nutshell?

Creating a Store

You create a store by defining its schema and setting default values. Similar to reducer or having state using hooks. For example, if you have a userPreferences, you can have an application-wide store with one branch (property) with the user preferences.

1interface MyAppState {
2 userPrefrerences: UserPreferences;
3 setUserPreference: (data: UserPreferences) => void;
6const useMyAppState = create<MyAppState>()(
7 devtools(
8 persist(
9 (set) => ({
10 userPrefrerences: {
11 theme: "light",
12 profile: { id: 0, name: "unknown" },
13 },
14 setUserPreference: (data) =>
15 set((state) => ({ userPreferences: data })),
16 }),
17 {
18 name: "useMyAppStateStore",
19 }
20 )
21 )

The first usage of the hook defined with create defines the store for the whole application. Hence, you do not need to wrap your application with a context (or provider pattern). Then, you call the hook that you defined in one or many of your components. In our example, it would be an invocation to useMyAppState. All future calls to the hook will get the information. When you set the state, the state does a copy automatically. However, you will need to create a copy as you go deeper like you do with Redux or with a React Context. For example, you can set a new setter for setting the profile that is a property deep in the store (under the user preference).

1interface MyAppState {
2 userPrefrerences: UserPreferences;
3 setUserPreference: (data: UserPreferences) => void;
4 setUserProfile: (data: UserProfile) => void;
7const useMyAppState = create<MyAppState>()(
8 devtools(
9 persist(
10 (set) => ({
11 userPrefrerences: {
12 theme: "light",
13 profile: { id: 0, name: "unknown" },
14 },
15 setUserPreference: (data) =>
16 set((state) => ({ userPreferences: data })),
17 },
18 setUserProfile: (data) =>
19 set((state) => ({ userPreferences: {...state.userPreference, profile: data}} })),
20 }
21 ),
22 {
23 name: "useMyAppStateStore",
24 }
25 )
26 )

Another typical use case is to derive value from your store's data. When using a React Context, you define a useMemo with dependency on the source of the data and then pass the memoized value to your context. With Zustand, you create a property.

1interface MyAppState {
2 userPrefrerences: UserPreferences;
3 setUserPreference: (data: UserPreferences) => void;
4 setUserProfile: (data: UserProfile) => void;
5 isNewUser: boolean;
8const useMyAppState = create<MyAppState>()(
9 devtools(
10 persist(
11 (set, get) => ({
12 userPrefrerences: {
13 theme: "light",
14 profile: { id: 0, name: "unknown" },
15 },
16 setUserPreference: (data) =>
17 set((state) => ({ userPreferences: data })),
18 setUserProfile: (data) =>
19 set((state) => ({
20 userPreferences: { ...state.userPreference, profile: data },
21 })),
22 get isNewUser() {
23 return get() === 0;
24 },
25 }),
26 {
27 name: "useMyAppStateStore",
28 }
29 )
30 )

How to Consume the Store Data?

Consuming is the act of calling the hook and defining which property to listen for a change. Very powerful since you control which elements need to change to cause a render. For example, if you are interested in getting the user name only, then only when the userPreferences is mutated will you get a render. If the MyAppState has many sibling properties to userPreferences, the component relying on userPreferences will remain stable and only get rendered in a limited way.

1// ... Snip, inside your React component
2const userName = useMyAppState((state) =>;


Debugging relies on the existing Redux tool. Looking back at the previous example, you see the create function calls devtools. That is a middleware that registers your changes like it would with Redux.

Another trick is that because these are functions, you can set breakpoint directly on the set of the desired setter and explore the data.

Single Store or Many Stores

While having a single store is excellent, I appreciate how easy it is to create many stores. I have one for my main application but also one for all the notifications, and one for other topics that could be extracted for many different projects in the future. Thus, I feel free to have only one store. Zustand contrives to adapt to your way of working instead of constraining your development.

Closing Notes

You can use Immer with Zustand if you desire -- or not. You can persist your store in the local storage -- or not. You can get fancy with middlewares and expand the features -- or not. Overall, Zustand is lightweight state management that opens into more complex scenarios if you desire. I focus on simple solutions, thus suitable for my case. The best is that you can be very accurate on how you impact rendering by having a central state that only triggers rendering on a very narrow set of data.