Zustand or Redux Store ?

Savindu Pasintha
4 min readJul 15, 2024

Based on my previous experience with Redux, Redux Saga, Mobx, and Flux, I find Zustand state management to be easier to use and understand.

Choosing the best state management solution for a React frontend can depend on the specific needs and complexity of your application. Here, we’ll compare Zustand and Redux, highlighting their strengths and potential use cases to help you make an informed decision.

Zustand

Pros

  1. Simplicity: Zustand is very simple to set up and use. It has a minimal API and avoids much of the boilerplate associated with Redux.
  2. Flexible API: Zustand’s API is flexible and can be used with or without React hooks.
  3. Lightweight: Zustand is lightweight, making it a good choice for performance-critical applications.
  4. Middleware Support: Zustand supports middleware for tasks like logging and persistence, though not as extensive as Redux.
  5. DevTools Integration: Zustand can integrate with Redux DevTools for state debugging.
  6. Support multiple middlewares like persist , immer …

Cons

  1. Smaller Ecosystem: Zustand has a smaller ecosystem compared to Redux, which might mean fewer ready-made solutions for common problems.
  2. Less Opinionated: Zustand is less opinionated, which is generally a pro but can be a con if you prefer a more structured and guided approach like Redux.

When to Use Zustand

  • Your application is relatively simple or you want to avoid boilerplate code.
  • You need a lightweight and flexible state management solution.
  • You are building a small to medium-sized application where simplicity and performance are more critical.

Click : https://github.com/savindu-pasintha/Reactjs-Utilities/tree/main/Zustand-State-Managment

// userStore.js
import create from 'zustand';

const useUserStore = create((set) => ({
user: null,
login: (userData) => set({ user: userData }),
logout: () => set({ user: null }),
}));

export default useUserStore;

// App.js
import React from 'react';
import useUserStore from './userStore';

function App() {
const { user, login, logout } = useUserStore((state) => ({
user: state.user,
login: state.login,
logout: state.logout,
}));

return (
<div>
{user ? (
<div>
<h2>Welcome, {user.name}</h2>
<button onClick={logout}>Logout</button>
</div>
) : (
<button onClick={() => login({ id: 1, name: 'John Doe' })}>Login</button>
)}
</div>
);
}

export default App;

Redux

Pros

  1. Predictable State Management: Redux provides a predictable state container, making it easier to understand how state changes over time.
  2. Middleware Support: Redux has a robust middleware system, allowing for powerful features like logging, crash reporting, asynchronous actions, and more.
  3. Large Ecosystem: Redux has a large ecosystem with many libraries and tools that integrate seamlessly with it, including Redux Toolkit, which simplifies common Redux tasks.
  4. DevTools: Redux DevTools provide an excellent debugging experience with time-travel debugging, state inspection, and action logging.

Cons

  1. Boilerplate Code: Traditional Redux requires a lot of boilerplate code, which can be cumbersome for smaller projects. However, this is mitigated by Redux Toolkit.
  2. Learning Curve: Redux can have a steep learning curve for beginners due to concepts like reducers, actions, and middleware.

When to Use Redux

  • Your application is complex with a lot of shared state across different components.
  • You need robust middleware support for handling side effects.
  • You want to take advantage of the extensive ecosystem and DevTools for debugging.

Click : https://github.com/savindu-pasintha/Reactjs-Utilities/tree/main/ReduxToolkit-State-Managment

// store.js
import { configureStore } from '@reduxjs/toolkit';
import userReducer from './userSlice';

const store = configureStore({
reducer: {
user: userReducer,
},
});

export default store;

// userSlice.js
import { createSlice } from '@reduxjs/toolkit';

const userSlice = createSlice({
name: 'user',
initialState: null,
reducers: {
login: (state, action) => action.payload,
logout: () => null,
},
});

export const { login, logout } = userSlice.actions;
export default userSlice.reducer;

// App.js
import React from 'react';
import { Provider, useSelector, useDispatch } from 'react-redux';
import store from './store';
import { login, logout } from './userSlice';

function App() {
const user = useSelector((state) => state.user);
const dispatch = useDispatch();

return (
<Provider store={store}>
<div>
{user ? (
<div>
<h2>Welcome, {user.name}</h2>
<button onClick={() => dispatch(logout())}>Logout</button>
</div>
) : (
<button onClick={() => dispatch(login({ id: 1, name: 'John Doe' }))}>Login</button>
)}
</div>
</Provider>
);
}

export default App;

Comparison Table

Conclution

As my experience, I felt we can replace our Redux with Zustand store without any issue and we can get the better performance more than the Redux and we no need to worry about Redux thunk like middleware for the managing async process.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet

Write a response