Redux vs Zustand for state management in React app

Redux vs Zustand for state management in React app

"A JS library for predictable and maintainable global state management" - Redux

"A small, fast, and scalable bearbones state management solution" - Zustand


Let us understand the concept of state management in React application first.

State management in React refers to the way data is managed and updated within a React application. React components can have their own local state (component state) which allows them to manage and update data without directly affecting other components.

Let us see practically how to implement a simple state management application with both Redux and Zustand individually to understand the differences .

Counter state with Redux(Centralised state store)

  1. Create store with configureStore() function

reduxStore.js

2. Create state slice with createSlice() function and add

{ counter1:100,counter2:200} as initial state

counterSlice.js

3. Add this reducer to redux store

reduxStore.js


4. Provide the redux store to Root/Parent component.

main.jsx


Now, this store is accessible by every component of the application.

5. Consume this store in component.

Note-Never access whole state(Both in Redux and in Zustand) in a Component. It leads to multiple re-renders. i.e. If a component needs counter1, then select only counter1 instead of the entire state which includes counter2 too. For example , if you write

let {counter1}=useSelector(state=>state) <- This is wrong

Then you can see this warning in console.


This warning is self explanatory . That selector will re-render a component either counter1 or counter2 is changed.

Let us create two components where one can consume counter1 & other counter2

TestReduxCounter1.jsx
TestReduxCounter2.jsx


Now, when counter1 is changed then , only TestReduxCounter1.jsx will be re-rendered. In the same way, for counter2 it is TestReduxCounter2.jsx.

In this way, the state management can be implemented using Redux in React application


Counter state with Zustand (Hook based state management)

  1. Create store using create() function of Zustand

useCounter.js


It's like a custom hook, so that name should start with "use".

2. Select counter1 & changeCounter1 function in a component & counter2 & changeCounter2 in another component.

Test1.jsx


Test2.jsx

That's it.


Note- I have added console statement in every component to check re-rendering


Observations

Redux:

  1. Centralised State Store: Redux provides a centralized store to manage the entire state of your application. It follows a single source of truth principle, where the entire application state is stored in a single immutable object tree
  2. Immutable Updates: Redux encourages immutable updates to the state. This ensures that state changes are predictable and helps in debugging and maintaining the application.
  3. Predictable State Changes: Redux enforces a strict unidirectional data flow and provides tools to monitor and log state changes, which helps in debugging and understanding application behavior.
  4. Community and Ecosystem: Redux has a large and active community with extensive documentation, tutorials, and middleware support. It's widely adopted in large-scale applications and complex state management scenarios.

Zustand

  1. Hook-based State Management: Zustand simplifies state management by eliminating the need for actions and reducers. State updates are handled directly within components using hooks.
  2. Immutability and Updates: Zustand encourages immutable updates using libraries like immer. This helps in maintaining a clean state update mechanism within components.
  3. Component-level State: Zustand encourages local state management within components. Each Zustand store can be scoped to a specific component or a group of related components.
  4. Scalability: While Zustand is suitable for small to medium-sized applications, its simplicity may make it less suitable for very large applications requiring complex state management patterns or extensive middleware support.


Choosing Between Redux and Zustand

  • Redux is ideal for large-scale applications where complex state management, time-travel debugging, and extensive middleware support are required.
  • Zustand is well-suited for smaller to medium-sized applications or components within larger applications, offering simplicity, performance optimisations, and direct integration with React hooks.


Kindly let me know if i miss any valid point in this.


Pushpalatha Medisetti

Associate Engineer at Cognizant Technologies

9 个月

Helpful ??

回复

要查看或添加评论,请登录

Rajesh T的更多文章

  • What is an API ?

    What is an API ?

    An API is a set of rules and protocols that allow different software applications to communicate with each other. Types…

    1 条评论
  • Simple state management in MEAN Application

    Simple state management in MEAN Application

    State management in Web apps is always challenging. Here, I have taken a simple e-learning app to demonstrate, state…

    1 条评论
  • Shallow copy vs Deep copy

    Shallow copy vs Deep copy

    Copy to an object can be created in many ways in JavaScript. Let us first understand difference between primitives and…

  • Units in CSS

    Units in CSS

    Absolute length units The absolute length units are fixed and a length expressed in any of these will appear as exactly…

  • Promises in JavaScript

    Promises in JavaScript

    The Promise object represents the eventual completion (or failure) of an asynchronous operation, and its resulting…

  • Callbacks in JavaScript

    Callbacks in JavaScript

    Most of us feel confusion while learning about asynchronous behavior of JavaScript. In this article i tried to explain…

  • Error handling in Express Application

    Error handling in Express Application

    Error Handling refers to how Express catches and processes errors that occur both synchronously and asynchronously…

    1 条评论
  • Storing images in MEAN stack application using "Cloudinary"

    Storing images in MEAN stack application using "Cloudinary"

    Saving images and videos of a web application is always challenging, because, performance of application matters.We…

    2 条评论
  • Connecting Angular application with Nodejs backend

    Connecting Angular application with Nodejs backend

    In this article, i will explain ,how to connect Angular Application with Nodejs backend. When we create a new angular…

  • Token based Authentication in ExpressJS Application

    Token based Authentication in ExpressJS Application

    HTTP is a stateless protocol. That means ,it cannot maintain state of previous requests.

社区洞察

其他会员也浏览了