DEV Community

Cover image for No Wonder why Zustand is Popular as Client-Side State Management libary
Rubi
Rubi

Posted on

No Wonder why Zustand is Popular as Client-Side State Management libary

I tested Zustand state management library in React these days. It was way less boilerplate code than Redux Toolkit, which is also a state management lib in React. If you've ever felt bogged down by the boilerplate required to get a simple global state working in Redux Toolkit (RTK), you aren't alone.

Zustand offical site

In Redux Toolkit, even a simple state change requires a fair amount of "ceremony." To get things running, you typically have to:

  • Create a Slice (defining initial state and reducers).
  • Configure the Store.
  • Wrap your application in a Provider to grant child components access.
  • Use multiple hooks (useDispatch and useSelector) just to interact with that state.

RTK

RTK

RTK

RTK
While RTK is powerful, this flow can feel like a lot of heavy lifting for smaller or medium-sized features.

In Zustand, I have to write only the store hook for state and functions ( which is like a reducer function in RTK) and directly use that hook in the component. The component directly applies state changes using that store hook, which is less complex than redux tool kits.

Zustand

Zustand

In conclusion, code written with Zustand is cleaner and easier to read than code written in Redux Toolkit. Since Zustand's purpose is for client-side state management, unlike the Redux toolkit, combining Zustand with TanStack Query, which is a great lib for handling server-side state managemnt would be a powerhouse combination.

Check out the code: I’ve put together a demo project comparing the two approaches here:
Cosmetic Store Demo on GitHub

Top comments (0)