DEV Community

Cover image for Zustand may be the one state-manager in the React space that gets all of these right.
Vitalii Sevastianov
Vitalii Sevastianov

Posted on

Zustand may be the one state-manager in the React space that gets all of these right.

Zustand is a state management library for React. It's a lightweight, fast, and simple way to manage state in your React applications. Zustand aims to provide a simple and efficient solution for managing state in React by using hooks and functional components.
Zustand provides a global state store that can be shared by multiple components and updated using actions. The state store is created using the create function and can be accessed using the useStore hook. Components that need access to the state can subscribe to changes in the state and re-render when the state changes.
Zustand offers a simpler and less verbose way of managing state compared to other popular state management libraries like Redux. It also emphasizes performance and ease of use, making it a popular choice among React developers.

Significant advantages

  • Lightweight and fast: Zustand is a lightweight library that doesn't add any additional overhead to your React application. It's also fast and optimized for performance, making it ideal for both small and large-scale React applications.

  • Simple and easy to use: Zustand uses a simple and straightforward API based on hooks and functional components, making it easy to learn and use.

  • Functional approach: Zustand takes a functional approach to state management, which makes it easier to reason about the state and how it affects the behavior of your components.

  • Flexible: Zustand is highly flexible and can be used in a variety of ways, depending on the needs of your application. You can use it for centralized state management, or for small-scale state management within a single component.

  • Strong performance: Zustand uses efficient algorithms to minimize the number of re-renders of components and ensure fast updates of the state.

  • Community support: Zustand is an actively maintained library with a strong and growing community of developers, making it easier to find help and resources when needed.

How you can use Zustand to manage the state

Here is a code example of how you can use Zustand state management library in a React component:

import React from 'react';
import { useStore } from 'zustand';

// create a store using the create method
const useCounterStore = create(set => ({
  count: 0,
  increment: () => set(state => ({ count: state.count + 1 })),
  decrement: () => set(state => ({ count: state.count - 1 }))
}));

function Counter() {
  // access the store using the useStore hook
  const { count, increment, decrement } = useStore(useCounterStore);

  return (
    <div>
      <h1>{count}</h1>
      <button onClick={increment}>+</button>
      <button onClick={decrement}>-</button>
    </div>
  );
}
Enter fullscreen mode Exit fullscreen mode

In this example, we create a store using the create method and define the initial state and actions. We then access the store using the useStore hook in the Counter component. The component subscribes to changes in the state and re-renders when the state changes.

You can also create multiple stores and access them in different components as needed. Additionally, you can use the set method to update the state, which triggers a re-render of all components that subscribe to the store.

Here's another example of how you can use Zustand to manage the state of a to-do list application:

import React from 'react';
import { useStore } from 'zustand';

// create a store using the create method
const useTodoStore = create(set => ({
  todos: [],
  addTodo: (todo) => set(state => ({ todos: [...state.todos, todo] })),
  removeTodo: (index) => set(state => ({
    todos: state.todos.filter((_, i) => i !== index)
  }))
}));

function TodoList() {
  // access the store using the useStore hook
  const { todos, addTodo, removeTodo } = useStore(useTodoStore);

  return (
    <div>
      <h1>Todo List</h1>
      <ul>
        {todos.map((todo, index) => (
          <li key={index}>
            {todo}
            <button onClick={() => removeTodo(index)}>Remove</button>
          </li>
        ))}
      </ul>
      <input type="text" placeholder="Add Todo" onKeyDown={(event) => {
        if (event.key === 'Enter') {
          addTodo(event.target.value);
          event.target.value = '';
        }
      }} />
    </div>
  );
}

Enter fullscreen mode Exit fullscreen mode

In this example, we create a store using the create method and define the initial state and actions for the to-do list. We then access the store using the useStore hook in the TodoList component. The component subscribes to changes in the state and re-renders when the state changes.

We use the addTodo action to add a new to-do item to the list, and the removeTodo action to remove a to-do item from the list. The component also contains an input field where the user can add a new to-do item, which updates the state and re-renders the component.

Here's an example of how you can use Zustand to manage the state of an asynchronous request in a React application:

import React from 'react';
import { useStore } from 'zustand';

// create a store using the create method
const useWeatherStore = create(set => ({
  city: '',
  temperature: 0,
  loading: false,
  error: null,
  setCity: (city) => set(state => ({ city })),
  setLoading: (loading) => set(state => ({ loading })),
  setTemperature: (temperature) => set(state => ({ temperature })),
  setError: (error) => set(state => ({ error }))
}));

async function fetchWeather(city) {
  try {
    const response = await fetch(`https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=API_KEY`);
    const data = await response.json();
    return data.main.temp;
  } catch (error) {
    throw error;
  }
}

function WeatherInfo() {
  // access the store using the useStore hook
  const { city, temperature, loading, error } = useStore(useWeatherStore);

  return (
    <div>
      <h1>Weather Info</h1>
      {loading && <p>Loading...</p>}
      {error && <p>Error: {error}</p>}
      {!loading && !error && (
        <p>
          City: {city}
          <br />
          Temperature: {temperature}
        </p>
      )}
    </div>
  );
}

function WeatherForm() {
  // access the store using the useStore hook
  const { city, setCity, setLoading, setTemperature, setError } = useStore(useWeatherStore);

  const handleSubmit = async (event) => {
    event.preventDefault();
    setLoading(true);
    setError(null);
    try {
      const temperature = await fetchWeather(city);
      setTemperature(temperature);
    } catch (error) {
      setError(error);
    } finally {
      setLoading(false);
    }
  };

  return (
    <div>
      <h1>Weather Form</h1>
      <form onSubmit={handleSubmit}>
        <input type="text" placeholder="City" value={city} onChange={(event) => setCity(event.target.value)} />
        <button type="submit">Submit</button>
      </form>
    </div>
  );
}

Enter fullscreen mode Exit fullscreen mode

In this example, we create a store using the create method and define the initial state and actions for the weather information. We also define a function fetchWeather that makes an asynchronous API request to retrieve the temperature for a given city.

The WeatherInfo component displays the current city, temperature, loading state, and error state. The WeatherForm component contains a form that allows the user to input the city and submit the form to retrieve the temperature. When the form is submitted, the component sets the loading state to true and makes the asynchronous API request using the fetchWeather function.

The choice of using Zustand

You might choose to use Zustand as a state management library in a React application for several reasons:

  1. Simplicity: Zustand is a simple and easy-to-use library, which makes it a good choice for small to medium-sized projects.

  2. Performance: Zustand uses functional updates, which are faster and less prone to errors compared to object updates. This makes Zustand a good choice for performance-critical applications.

  3. Customizability: Zustand provides a flexible and customizable API that allows you to define your own state management logic. You can define custom actions, selectors, and middleware to fit your specific needs.

  4. TypeScript support: Zustand is fully compatible with TypeScript, which makes it a good choice for projects that use TypeScript.

  5. No React-specific dependencies: Zustand does not have any dependencies on React, which makes it a good choice for projects that use other libraries or frameworks besides React.

Ultimately, the choice of using Zustand or any other state management library depends on your specific project requirements and personal preferences.

Conclusion

In conclusion, Zustand is a highly regarded state management library for React applications, offering a number of benefits that make it an attractive choice for developers.

One of the key benefits of using Zustand is its simplicity. The library is easy to use and requires minimal setup, making it an ideal choice for small to medium-sized projects where developer time is at a premium.

Another major benefit of Zustand is its performance. The library uses functional updates, which are faster and less prone to errors compared to object updates, making it a great choice for performance-critical applications.

Zustand also offers a high degree of customizability. The library's API provides developers with the flexibility to define their own state management logic, including custom actions, selectors, and middleware. This makes Zustand an excellent choice for projects with specific requirements or constraints.

Additionally, Zustand is fully compatible with TypeScript, which makes it a great choice for projects that use TypeScript. The library also does not have any dependencies on React, which makes it a good choice for projects that use other libraries or frameworks besides React.

Despite these benefits, it's important to remember that state management libraries like Zustand are just one tool in the toolkit of a React developer. The choice of whether or not to use Zustand, or another state management library, depends on the specific needs and requirements of your project. Before making a decision, it's important to weigh the benefits and drawbacks and make an informed decision based on your project's specific requirements.

Top comments (0)