<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Mohammad Moeenuddin</title>
    <description>The latest articles on Forem by Mohammad Moeenuddin (@im_10moin).</description>
    <link>https://forem.com/im_10moin</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3151792%2F65566ce7-2a81-4874-ac6b-48ae64c98434.jpg</url>
      <title>Forem: Mohammad Moeenuddin</title>
      <link>https://forem.com/im_10moin</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/im_10moin"/>
    <language>en</language>
    <item>
      <title>Zustand 101: Beginner's Guide</title>
      <dc:creator>Mohammad Moeenuddin</dc:creator>
      <pubDate>Tue, 17 Jun 2025 19:18:55 +0000</pubDate>
      <link>https://forem.com/im_10moin/zustand-101-beginners-guide-4h20</link>
      <guid>https://forem.com/im_10moin/zustand-101-beginners-guide-4h20</guid>
      <description>&lt;p&gt;Have you ever wished for a state management library that's as simple as &lt;code&gt;useState&lt;/code&gt; but works globally across your entire React application &lt;strong&gt;?&lt;/strong&gt; Are you looking for a state management library that doesn't require tons of &lt;code&gt;boilerplate code&lt;/code&gt; &lt;strong&gt;?&lt;/strong&gt; Then this article is for you, it will show you how to handle React state using a simple external library called &lt;em&gt;&lt;u&gt;Zustand&lt;/u&gt;&lt;/em&gt;.&lt;br&gt;
&lt;br&gt;
Indeed! &lt;u&gt;State and state management&lt;/u&gt; have always been the backbone of React apps, determining when and how components re-render. While React's built-in &lt;code&gt;useState&lt;/code&gt; hook works perfectly for local component state, things get worse when you need to share state across multiple components.&lt;/p&gt;

&lt;p&gt;As your React app grows and becomes more complex, you'll find yourself facing challenges like &lt;em&gt;&lt;code&gt;prop drilling&lt;/code&gt;&lt;/em&gt;, &lt;em&gt;&lt;code&gt;context provider hell&lt;/code&gt;&lt;/em&gt;, and the need for a cleaner way to manage state.&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  React State Management
&lt;/h2&gt;

&lt;p&gt;&lt;br&gt;
You can manage the state of a React application in several ways.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1.Built-in React State Solutions&lt;/strong&gt;: Native hooks like &lt;em&gt;&lt;code&gt;useState&lt;/code&gt;&lt;/em&gt;, &lt;em&gt;&lt;code&gt;useReducer&lt;/code&gt;&lt;/em&gt;, &lt;em&gt;&lt;code&gt;useContext&lt;/code&gt;&lt;/em&gt;, and &lt;em&gt;&lt;code&gt;useMemo&lt;/code&gt;&lt;/em&gt; provide foundational state management capabilities.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2.Indirect State Managers:&lt;/strong&gt; There are external libraries such as &lt;code&gt;React Router&lt;/code&gt; and &lt;code&gt;React Query&lt;/code&gt;. They are not primarily used to manage the state. Yet, when combined with a native hook, they can handle the state well.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3.Dedicated State Management Libraries:&lt;/strong&gt; Purpose-built solutions like &lt;code&gt;Redux&lt;/code&gt;, &lt;code&gt;Zustand&lt;/code&gt;, &lt;code&gt;Jotai&lt;/code&gt;, and &lt;code&gt;Valtio&lt;/code&gt; are designed exclusively for comprehensive state management across your application.&lt;br&gt;
&lt;/p&gt;






&lt;h2&gt;
  
  
  What is Zustand ?
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;Zustand&lt;/code&gt; is a lightweight state management library. It is &lt;em&gt;compact, fast, and scalable&lt;/em&gt;. It's simple with &lt;em&gt;little boilerplate code&lt;/em&gt;. It doesn’t rely on a provider. As a result, you don’t have to code as much React logic, which may lessen the things we tend to forget. It works based on simplified flux principles and primarily makes use of hooks.&lt;/p&gt;

&lt;h5&gt;
  
  
  Why Zustand ?
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;It is faster than context. It lets you subscribe to specific slices of state, reducing unnecessary re-renders.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It does state merging by default. Consider updating a single property of an object state, &lt;code&gt;{name: 'John', age: 25}&lt;/code&gt;. You can directly set &lt;code&gt;{age: 26}&lt;/code&gt;. Zustand will merge the data for you. You don’t have to distribute the old state and update properties like &lt;code&gt;{…state, age: 26}&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It is extendable by default. Thus, you can use a variety of middleware types such as persist, immer, devtools ... etc.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;It is less opinionated and rigid. You don’t have to stick to one way of doing things. Even though there is a recommended approach, you are not required to adopt it.&lt;br&gt;
&lt;/p&gt;




&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;End of &lt;del&gt;theory&lt;/del&gt;. Let's get your hands dirty now!&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;br&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Zustand with ReactJs
&lt;/h2&gt;

&lt;p&gt;&lt;br&gt;
Let's create a React project to show how to manage the states using Zustand. Let's consider a simple Counter application to understand Zustand better. The steps are listed below.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Create a React application&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Create your React application using the below command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm create vite@latest 'project-name' 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;br&gt;
&lt;strong&gt;2. Install Zustand Dependency&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Go to the project’s directory and install the zustand dependency to manage the React state.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install
npm i zustand
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;br&gt;
&lt;strong&gt;3. Let's Start&lt;/strong&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Let’s first build a basic counter using React's local state to understand the problem Zustand solves.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's create a Counter application with simple increase and decrease button as seen in the below image.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4762pjgg0m3gxoo2rsi9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4762pjgg0m3gxoo2rsi9.png" alt="Counter app" width="581" height="362"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;refer below code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//App.jsx

import { useState } from 'react'
import './App.css'

function App() {

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;Counter/&amp;gt;
    &amp;lt;/div&amp;gt;
  )
}

function Counter(){
  const [count, setCount] = useState(0)

  return(
    &amp;lt;div&amp;gt;
      &amp;lt;CurrentCount count={count}/&amp;gt;
      &amp;lt;br /&amp;gt;
      &amp;lt;div className='flex'&amp;gt;
        &amp;lt;Increase setCount={setCount}/&amp;gt;
        &amp;lt;Decrease setCount={setCount}/&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
  )
}


function CurrentCount({count}){
  return(
    &amp;lt;div className='text-8xl'&amp;gt;
      {count}
    &amp;lt;/div&amp;gt;
  )
}

function Decrease({setCount}){
  return(
    &amp;lt;div className='m-3'&amp;gt;
      &amp;lt;button onClick={()=&amp;gt;{setCount(c=&amp;gt;c-1)}}&amp;gt;Decrease Count&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  )
}

function Increase({setCount}){
  return(
    &amp;lt;div className='m-3'&amp;gt;
      &amp;lt;button onClick={()=&amp;gt;{setCount(c=&amp;gt;c+1)}}&amp;gt;Increase Count&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  )
}
export default App
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;ul&gt;
&lt;li&gt;Upon clicking either of the increase or decrease button, only the count part should re-render, but this is not the case here, on clicking either of the buttons, the count, both of the button and the whole app component re-renders unnecessarily, as seen in below images.
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4nd3frhh6xdclatamnn7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4nd3frhh6xdclatamnn7.png" alt="on clicking increase both the buttons re-render" width="489" height="334"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fru3z55wcqx9r4dc3jp5p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fru3z55wcqx9r4dc3jp5p.png" alt="on clicking decrease both the buttons re-render" width="477" height="293"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;So, now let's use Zustand to manage the state and resolve this issue.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;br&gt;
&lt;strong&gt;4. Using Zustand to Fix Re-Renders&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let's create a store. Refer the below code to create &lt;code&gt;Store.jsx&lt;/code&gt; file.&lt;br&gt;
&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { create } from "zustand";

export const useCounterStore = create((set)=&amp;gt;({
    count:0,
    increment:()=&amp;gt;{
        set((state)=&amp;gt;({count:state.count+1}))
    },
    decrement:()=&amp;gt;{
        set((state)=&amp;gt;({count:state.count-1}))
    }
}))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Zustand store is a hook, which is why &lt;code&gt;useCounterStore&lt;/code&gt; is the component name. &lt;code&gt;create&lt;/code&gt;is the method used to create the store. The &lt;code&gt;store&lt;/code&gt;is the sole source of truth that each component shares. The function &lt;code&gt;set&lt;/code&gt;is used to modify the state of a variable or object. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The state object of the store in the example contains the following field and methods: &lt;code&gt;count&lt;/code&gt;, which stores the current count of the counter and &lt;code&gt;increment&lt;/code&gt; and &lt;code&gt;decrement&lt;/code&gt; methods which increase and decrease the count respectively.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &lt;code&gt;increment&lt;/code&gt; and &lt;code&gt;decrement&lt;/code&gt; methods use &lt;code&gt;set&lt;/code&gt; to modify the state of the count.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;br&gt;
&lt;strong&gt;4. Bind the component with your store&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let's modify the code inside &lt;code&gt;App.jsx&lt;/code&gt; using the &lt;code&gt;useCounterStore&lt;/code&gt;.&lt;br&gt;
&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import './App.css'
import { useCounterStore } from './store'

function App() {
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;CurrentCount/&amp;gt;
      &amp;lt;Buttons/&amp;gt;
    &amp;lt;/div&amp;gt;
  )
}


function CurrentCount(){
  const currcount=useCounterStore((state)=&amp;gt;state.count);
  return(
    &amp;lt;div className='text-8xl'&amp;gt;
      {currcount}
    &amp;lt;/div&amp;gt;
  )
}

function Buttons(){
const increase =useCounterStore((state)=&amp;gt;state.increment);
  return (
    &amp;lt;div className='flex'&amp;gt;
      &amp;lt;div className='m-3'&amp;gt;&amp;lt;button onClick={increase}&amp;gt;Increase&amp;lt;/button&amp;gt;&amp;lt;/div&amp;gt;
      &amp;lt;div className='m-3'&amp;gt;&amp;lt;button onClick={useCounterStore((state)=&amp;gt;state.decrement)}&amp;gt;Decrease&amp;lt;/button&amp;gt;&amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
  )
}
export default App
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;App.jsx&lt;/code&gt; contains three components i.e. &lt;code&gt;App&lt;/code&gt;, &lt;code&gt;CurrentCount&lt;/code&gt; and &lt;code&gt;Buttons&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;App&lt;/code&gt; is the parent component which contains both the &lt;code&gt;CurrentCount&lt;/code&gt; and &lt;code&gt;Buttons&lt;/code&gt; components.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;CurrentCount&lt;/code&gt; component subscribes only to &lt;code&gt;count&lt;/code&gt;from the &lt;code&gt;useCounterStore&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;Buttons&lt;/code&gt; component subscribes only to the &lt;code&gt;increment&lt;/code&gt; and &lt;code&gt;decrement&lt;/code&gt; methods and does not re-render when the value of count changes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now, on clicking either of the increase or decrease button, only the count part re-renders and there are no unnecessary re-renders, as seen in below images.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1v0wtrdxqycvq1kn4lmz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1v0wtrdxqycvq1kn4lmz.png" alt="on clicking increase only count re-render" width="438" height="253"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbr1dzcylebm2monuqt1z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbr1dzcylebm2monuqt1z.png" alt="on clicking decrease only count re-render" width="333" height="216"&gt;&lt;/a&gt;&lt;br&gt;
&lt;br&gt;
Neither of the button's re-render unnecessarily when the value of count changes. &lt;/p&gt;





&lt;blockquote&gt;
&lt;p&gt;Since the &lt;em&gt;Zustand store&lt;/em&gt; is a &lt;code&gt;hook&lt;/code&gt;, you can use it anywhere. In contrast to &lt;em&gt;Redux&lt;/em&gt; or &lt;em&gt;Redux Toolkit&lt;/em&gt;, &lt;code&gt;no context provider&lt;/code&gt; is required. Simply select your state, and the component will re-render when the state changes. We must give a selector to the &lt;code&gt;useCounterStore&lt;/code&gt; to get only the desired slice.&lt;br&gt;
&lt;br&gt;
&lt;/p&gt;

&lt;h3&gt;
  
  
  We are not done yet! Zustand has a big advantage: Middlewares.
&lt;/h3&gt;

&lt;h4&gt;
  
  
  which we'll be covering in the upcoming blog.
&lt;/h4&gt;


&lt;/blockquote&gt;






&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;As we mentioned in this article, state management is critical in React applications. As your application grows, you will have to pick a strong way to manage its state. The Zustand library is an ideal remedy for managing your React state. It is much simpler and has less boilerplate than Redux. You can also explore different state management libraries to find the right one for your application.&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://zustand.docs.pmnd.rs/getting-started/introduction" rel="noopener noreferrer"&gt;https://zustand.docs.pmnd.rs/getting-started/introduction&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://youtu.be/_ngCLZ5Iz-0?si=ntdxgnViME_fkZwf" rel="noopener noreferrer"&gt;https://youtu.be/_ngCLZ5Iz-0?si=ntdxgnViME_fkZwf&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>beginners</category>
      <category>react</category>
      <category>zustand</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
