DEV Community

Cover image for Supercharging Your React State Management with Zustand
Yevhen Kozachenko ๐Ÿ‡บ๐Ÿ‡ฆ
Yevhen Kozachenko ๐Ÿ‡บ๐Ÿ‡ฆ

Posted on • Originally published at ekwoster.dev

Supercharging Your React State Management with Zustand

Supercharging Your React State Management with Zustand

When it comes to managing state in modern React applications, developers are often overwhelmed with choices. From Redux to Context API to Zustand, there are many solutions tailored for different levels of complexity and use cases. Today, we're diving into one of the most elegant and minimalistic state management libraries in the React ecosystem: Zustand.

Created by the mind behind Jotai and React-spring, Zustand provides a small, fast, and scalable solution for global and local state management. In this blog post, weโ€™ll explore what makes Zustand stand out, how it compares to other libraries, and how you can implement it in your next project.

๐ŸŒฑ What is Zustand?

Zustand (which means "state" in German) is a small, fast, and scalable bearbones state-management solution using simplified Flux principles. The library has no boilerplate, supports multiple stores, server-side rendering, and works well with TypeScript.

At its core, Zustand gives you a global store that is intuitive to use and doesnโ€™t require any context providers or setup overhead.

Key Features:

  • Minimal API (intuitive and easy to learn)
  • No Provider wrapper required
  • Works in every React environment
  • Seamless integration with TypeScript
  • Easy to persist and rehydrate state
  • React Native friendly

๐Ÿง  Redux vs Context API vs Zustand

Feature Redux Context API Zustand
Boilerplate High Low Very Low
Learning Curve Medium-High Low Very Low
Performance Medium Low-Medium High
Async Actions Via Middleware Not Built-In Built-In
Typing (TS Support) Manual Manual Automatic

Zustand is ideal for projects that need global state without the heavy lifting associated with Redux. Think of it as having the power of Redux with the simplicity of the useState hook.

โš™๏ธ How to Use Zustand

Letโ€™s walk through creating a simple counter application using Zustand.

Step 1: Installation

npm install zustand
Enter fullscreen mode Exit fullscreen mode

Or using yarn:

yarn add zustand
Enter fullscreen mode Exit fullscreen mode

Step 2: Creating a Store

// store/counterStore.js
import { create } from 'zustand'

export const useCounterStore = create((set) => ({
  count: 0,
  increment: () => set((state) => ({ count: state.count + 1 })),
  decrement: () => set((state) => ({ count: state.count - 1 })),
  reset: () => set({ count: 0 }),
}))
Enter fullscreen mode Exit fullscreen mode

Step 3: Using the Store in a Component

import React from 'react'
import { useCounterStore } from './store/counterStore'

const Counter = () => {
  const { count, increment, decrement, reset } = useCounterStore()

  return (
    <div>
      <h1>Count: {count}</h1>
      <button onClick={increment}>+</button>
      <button onClick={decrement}>-</button>
      <button onClick={reset}>Reset</button>
    </div>
  )
}

export default Counter
Enter fullscreen mode Exit fullscreen mode

No need for Redux actions or reducers, no useReducer or useContext boilerplate. Zustand gives you direct and instant access to your state.

๐Ÿš€ Advanced Features

Partial Selection and Performance

Zustand supports selectors to subscribe only to specific parts of the state to minimize re-renders:

const count = useCounterStore((state) => state.count)
Enter fullscreen mode Exit fullscreen mode

Middleware Support

Zustand allows you to enhance stores with middleware like logging, persistency, or immutability. For example, adding localStorage persistence:

import { create } from 'zustand'
import { persist } from 'zustand/middleware'

export const useCounterStore = create(persist(
  (set) => ({
    count: 0,
    increment: () => set((state) => ({ count: state.count + 1 })),
  }),
  {
    name: 'counter-store', // unique name
    getStorage: () => localStorage, // define persisting mechanism
  }
))
Enter fullscreen mode Exit fullscreen mode

Async Actions Support

You can perform async actions directly inside your store:

const useDataStore = create((set) => ({
  data: [],
  fetchData: async () => {
    const res = await fetch('/api/data')
    const json = await res.json()
    set({ data: json })
  }
}))
Enter fullscreen mode Exit fullscreen mode

๐Ÿงช Unit Testing Zustand

State stores can be easily tested since they are plain JavaScript functions:

import { useCounterStore } from './store/counterStore'

describe('Counter Store', () => {
  it('increments the count', () => {
    useCounterStore.getState().increment()
    expect(useCounterStore.getState().count).toBe(1)
  })
})
Enter fullscreen mode Exit fullscreen mode

๐Ÿงพ Use Cases for Zustand

  • Global UI state (modals, notifications)
  • Auth state
  • Theme togglers
  • Global configuration
  • Cross-component shared data

Zustand fits great in both small and large applications, especially when you want pragmatic and high-performance state management without the ceremony.

๐Ÿง  Bonus: Zustand with TypeScript

Zustand has excellent TypeScript support. Hereโ€™s the same store typed:

interface CounterState {
  count: number
  increment: () => void
  decrement: () => void
}

export const useCounterStore = create<CounterState>((set) => ({
  count: 0,
  increment: () => set((state) => ({ count: state.count + 1 })),
  decrement: () => set((state) => ({ count: state.count - 1 })),
}))
Enter fullscreen mode Exit fullscreen mode

๐Ÿงฉ Zustand in the Real World

Companies love Zustand for its minimal API and focus on performance. It works seamlessly in big applications with dynamic requirements like dashboards, admin panels, and data-driven apps.

Also, tools like Valtio, Jotai, and Recoil share similar goals but target different use casesโ€”Zustand hits a sweet spot for most applications.

๐ŸŽฏ Conclusion

Zustand is a modern approach to state management that gives you the best of Reactโ€™s simplicity and the power of global stores without the overhead. If youโ€™re looking for a quick, reliable, and minimal tool to manage your application state in React or React Native projects, give Zustand a try.

๐Ÿ“š Resources


Happy Coding! ๐Ÿป

๐Ÿ› ๏ธ Looking to build a fast and scalable web app with cutting-edge tools like Zustand? We offer expert frontend development services to help bring your vision to life.

Top comments (3)

Collapse
 
leob profile image
leob • Edited

Are there ANY drawbacks to Zustand? If not, then why have we been having endless discussions about the pros and cons of Redux, versus Context, versus whatnot, for years on end? Seems a waste of time - just declare Zustand "the standard", and call it a day!

Collapse
 
ekwoster profile image
Yevhen Kozachenko ๐Ÿ‡บ๐Ÿ‡ฆ

Zustand is awesome, but not totally without trade-offs. Redux still has stronger devtools and a huge ecosystem, and some teams stick with it because they already know it well. Zustand is simpler and great for most apps, but for really large/complex ones, Reduxโ€™s strict patterns can help. So yeah โ€” no silver bullet, just the right tool for the job ๐Ÿ™‚

Collapse
 
mst_aktgsissvisbss_613ff profile image
Info Comment hidden by post author - thread only accessible via permalink
MST Aktgsis Svisbss

add my snapchat ID
lillysutton7508

Some comments have been hidden by the post's author - find out more