Logo
Services
Industries
Technologies
About
Case Study
Blog

Let's Build Your Tech Vision

120+ expert engineers & designers in AI, Web, Mobile.

General Inquiries

hr@mediusware.com+880 1897-661850-51

Discuss Services

sales@mediusware.com+880 1750-020408

Mediusware 2015 - 2026. All Rights Reserved

Quick Links

Our StoriesLeadershipBlogsContactCareerCSR Terms & ConditionsPrivacy Policy

Services

Software DevelopmentWeb DevelopmentMobile App DevelopmentE-commerce DevelopmentQA & DevOpsDigital MarketingArtificial IntelligenceBranding & Visualization

Technology

React.jsVue.jsAngular jsLaravelDjangoFlutterRustReact Native

Industries

RetailE-learningTravelHealthcareLogisticsEntertainmentFintechReal Estate
Blog/
  1. Introduction to Zustand
  2. Why Choose Zustand?
  3. Getting Started with Zustand
  4. Conclusion: The Power of Simplicity
ChatGPT
ChatGPT
Google AI Studio
Google AI Studio
Claude
Claude
Grok
Grok
Perplexity
Perplexity

Zustand: A Lightweight State Management Library for React

  • Zustand offers a minimal API, making state management simpler and faster than alternatives like Redux.

  • It provides efficient performance with selective re-rendering, preventing unnecessary updates.

  • Zustand can scale from simple state management in small projects to handling complex asynchronous tasks.

  • Its flexibility allows integration with React and other JavaScript frameworks for versatile use.

Zustand: A Lightweight State Management Library for React image
Navigate
  1. Introduction to Zustand
  2. Why Choose Zustand?
  3. Getting Started with Zustand
  4. Conclusion: The Power of Simplicity

Introduction to Zustand

Zustand is a small, fast, and flexible state management library designed to manage state in React applications. While libraries like Redux or MobX offer powerful solutions for managing complex global states, they often come with a significant learning curve and boilerplate. Zustand provides a simpler alternative, with a minimal API that lets you manage global or local state with ease.

 

With Zustand, you can manage your application's state without the need for reducers, middleware, or actions. Zustand stores are powerful enough to handle large-scale applications, yet simple enough to implement in smaller projects without unnecessary overhead.

Why Choose Zustand?

  • Simplicity: Zustand has a minimalistic API that's easy to learn and use, making it perfect for small to medium-sized projects.

  • Performance: Unlike React's Context API, Zustand doesn’t suffer from unnecessary re-renders. It only re-renders components that consume the specific state slice that has changed.

  • Scalability: It’s lightweight yet powerful enough to handle more complex state management needs, such as asynchronous actions and middleware.

  • Flexibility: Zustand is framework-agnostic and can be used in any JavaScript project, not just React.

Getting Started with Zustand

Let’s dive into some practical examples. In this section, we’ll show how to install Zustand and build a simple counter application.

 

1. Installation

First, install Zustand via npm or yarn:

$ npm install zustand

Or, if you prefer yarn:

$ yarn add zustand

Once installed, you can start building your store.

 

2. Creating a Zustand Store

Creating a store in Zustand is simple. You use the create function provided by Zustand to define your state and any actions to manipulate that state.

 

Example: A Simple Counter Store

import create from 'zustand';

const useStore = create((set) => ({
    count: 0,
    increase: () => set((state) => ({ count: state.count + 1 })),
    decrease: () => set((state) => ({ count: state.count - 1 })),
}));

In this example, we create a store that contains a count state, and two actions, increase and decrease, to modify that state. The set function provided by Zustand allows us to update the store.

 

Explanation:

  • count: The initial state, set to 0.
  • increase: A function that increases the count by 1.
  • decrease: A function that decreases the count by 1.

 

3. Consuming the Store in Components

Now that we have our store, let’s see how to use it in a component. Zustand makes consuming state in components extremely simple with its hook-based approach.

 

Example: A Simple Counter Component

import React from 'react';
import { useStore } from './store';

const Counter = () => {
    const { count, increase, decrease } = useStore((state) => ({
        count: state.count,
        increase: state.increase,
        decrease: state.decrease,
    }));

    return (
        <div>
            <h1>Count: {count}</h1>
            <button onClick={increase}>Increase</button>
            <button onClick={decrease}>Decrease</button>
        </div>
    );
};

export default Counter;

Explanation:

  • We use the useStore hook to extract the count, increase, and decrease values from our Zustand store.

  • Zustand ensures that only the components that use specific slices of the state will re-render when that state changes. This results in fewer unnecessary re-renders compared to using React's Context API.

  • The increase and decrease functions are called when the buttons are clicked, updating the state and causing the component to re-render with the new count.

 

4. Managing Complex State

Zustand isn’t just for simple state. You can also manage more complex state, including arrays, objects, and even asynchronous actions.

 

Example: Managing an Array of Todos

Let’s extend the example to manage a list of todo items.

import create from 'zustand';

const useTodoStore = create((set) => ({
    todos: [],
    addTodo: (newTodo) => set((state) => ({ todos: [...state.todos, newTodo] })),
    removeTodo: (todoId) => set((state) => ({
        todos: state.todos.filter((todo) => todo.id !== todoId)
    })),
}));

Explanation:

  • todos: An array that stores all todo items.

  • addTodo: A function that adds a new todo to the array.

  • removeTodo: A function that removes a todo from the array based on its ID.


Consuming the Todo Store in Components

import React, { useState } from 'react';
import { useTodoStore } from './todoStore';

const TodoApp = () => {
    const [newTodo, setNewTodo] = useState('');
    const { todos, addTodo, removeTodo } = useTodoStore();

    const handleAddTodo = () => {
        if (newTodo) {
            addTodo({ id: Date.now(), text: newTodo });
            setNewTodo('');
        }
    };

    return (
        <div>
            <h1>Todo List</h1>
            <input
                type="text"
                value={newTodo}
                onChange={(e) => setNewTodo(e.target.value)}
                placeholder="Enter a new todo"
            />
            <button onClick={handleAddTodo}>Add Todo</button>
            <ul>
                {todos.map((todo) => (
                    <li key={todo.id}>
                        {todo.text}
                        <button onClick={() => removeTodo(todo.id)}>Remove</button>
                    </li>
                ))}
            </ul>
        </div>
    );
};

export default TodoApp;

In this example, the TodoApp component consumes the useTodoStore hook to manage a list of todos. Zustand efficiently handles adding and removing todos while ensuring that only the necessary parts of the UI re-render when state changes.

 

5. Async Actions with Zustand

Zustand supports async actions as well. Let’s look at how to fetch data using an asynchronous action within a Zustand store.

 

Example: Async Action for Fetching Data

import create from 'zustand';

const useDataStore = create((set) => ({
    data: [],
    loading: false,
    fetchData: async () => {
        set({ loading: true });
        const response = await fetch('https://api.example.com/data');
        const result = await response.json();
        set({ data: result, loading: false });
    },
}));

Explanation:

  • loading: A boolean flag to indicate whether the data is being fetched.

  • fetchData: An asynchronous function that fetches data from an API and updates the state.

 

Consuming Async State in a Component

import React, { useEffect } from 'react';
import { useDataStore } from './dataStore';

const DataComponent = () => {
    const { data, loading, fetchData } = useDataStore();

    useEffect(() => {
        fetchData();
    }, [fetchData]);

    if (loading) return <div>Loading...</div>;

    return (
        <ul>
            {data.map((item) => (
                <li key={item.id}>{item.name}</li>
            ))}
        </ul>
    );
};

export default DataComponent;

Conclusion: The Power of Simplicity

Zustand is a lightweight but powerful state management tool that simplifies state management without compromising flexibility. Its minimal API and optimized performance make it a great choice for developers looking to build fast, scalable React applications with a clean state management architecture.

 

If you’ve been overwhelmed by more complex state management libraries like Redux, give Zustand a try. Its simplicity and performance can transform the way you manage state in your React projects, helping you stay productive and focused on building great features.

Frequently Asked Questions

Zustand is much simpler and more lightweight than Redux. While Redux requires setting up reducers, actions, and middleware, Zustand uses a minimal API with just a store and functions to update the state. It doesn’t rely on boilerplate or action types, making it faster to implement and easier to learn. Redux is often more suitable for complex state management in larger apps, while Zustand is great for simpler, more streamlined use cases, though it can scale well for larger apps too.

Author
By Mediusware Editorial Team

Content Team at Mediusware

We are the Mediusware Editorial Team, passionate about crafting insightful content on technology, software development, and industry trends. Our mission is to inform, inspire, and engage our audience with well-researched articles and thought leadership pieces. With a deep understanding of the tech landscape, we aim to be a trusted source of knowledge for professionals and enthusiasts alike.

Get the best of our content straight to your inbox!

By submitting, you agree to our privacy policy.

Introduction to Zustand
Why Choose Zustand?
Getting Started with Zustand
Conclusion: The Power of Simplicity
Navigate
Introduction to ZustandWhy Choose Zustand?Getting Started with ZustandConclusion: The Power of Simplicity

Get the best of our content straight to your inbox!

By submitting, you agree to our privacy policy.

Featureblogs

01Nearshore Staff Augmentation: Unlocking Global Talent02Branding & Visualization: Turning Short-Term Marketing into Long-Term Identity03The Future of NLP: Key Trends04Is Golang Still the Right Choice in 2025?05Computer-Assisted Learning: The Future of Education

Authorblogs

01Application Software: Types, Benefits, and Future Trends02What Is Wireframing? A Practical Guide03ChatGPT vs. Gemini vs. Claude vs. Copilot vs. Perplexity: What Matters for Real Work042025 Global Recognition Award: Mediusware’s Leadership in Innovation and Impact05React Vulnerability: Critical Security Flaw in React Server Components