How do I manage state with Zustand instead of Redux?

giocatore77 IT 🌱 Newbie 👁 113 ⚑ Report Programming

I'm migrating a React project from Redux to Zustand because Redux seems like overkill for what I need. Zustand looks lighter and simpler, but I want to understand if it loses anything in terms of functionality or debugging. Has anyone already used it in production?

2 answers

★ Best answer

The main advantage of Zustand is really the simplicity: you create a store with `create()`, define your state and actions, and use the hook directly in your components without providers or boilerplate. You don't lose important functionality, just Redux's complexity - for debugging you have tools like Zustand DevTools that work well, and if your state grows so much that you need something more, it means maybe Redux wasn't wrong, but in most modern React projects Zustand holds up great in production because it's lightweight and performant.

Don't just start by copying Redux's mental model into Zustand - Zustand's strength is actually its simplicity, and if we recreate the same complexity we've missed the point. The basic shift is: in Redux you defined actions, reducers, dispatch; in Zustand you directly define functions in your store that modify the state. Concrete example: instead of having `setCounter` as an action that returns an action object, in Zustand you write directly `set(state => ({count: state.count + 1}))`. The store becomes a create function that returns a hook, and you use it like `const count = useStore(state => state.count)`.

What you might lose: Redux has fantastic dev tools and a huge middleware ecosystem. Zustand has dev tools too (the devtools package exists), but it's less integrated by default - but honestly for most projects it's more than enough. Debugging is a bit less rich, but reading the state is immediate. What you gain is less boilerplate and a much flatter learning curve. I've seen quite a few people using it in production without issues, especially in mid-sized apps; Redux is still better if you have truly complex state with intricate dependencies between actions.

Practically speaking: start writing simple stores and test them as you migrate. Don't refactor everything at once. Zustand handles immutability well (with Immer if needed), deals with localStorage easily through plugins, and if after years you discover you needed something heavier, the Zustand code is still readable for whoever comes after.

Your answer

Log into answer.