State Management With Zustand

Written by
MacHamza Kargin
Published on
--
Views
1,167
Comments
2
State Management With Zustand

TL;DR

Zustand is an ultra-lightweight, zero-boilerplate state management library for React. It handles global state effortlessly without Redux-level complexity. Setup is one line, creating a store takes 5-10 lines. In the best React app structures, stores live in a dedicated folder or alongside features for clean organization. Compared to Redux, Context, or MobX: less code, better performance, fewer headaches.

Zustand repo: GitHub

Preface

Hey buddy, State management in React can be a real pain, right? Context API falls short on bigger apps, Redux feels like overkill with all the ceremony. I've been through the same struggles, trying everything until I landed on Zustand. It's simple, battle-tested in production, and just works. This guide keeps things practical and friendly—no walls of text or unnecessary deep dives. We'll cover why Zustand rocks and how to structure it cleanly in a real app..

Introduction

What is Zustand?

Zustand (German for "state") is a minimal state management library for React. No actions, reducers, or middleware boilerplate like Redux. You just use a single create function to define a store and access it anywhere with hooks. Key wins:

  • Almost zero boilerplate

  • Selectors let you subscribe only to what you need → no unnecessary re-renders

  • Built-in middleware support (persist, devtools, immer, etc.)

  • Excellent TypeScript support

  • Tiny size (~1 KB gzipped)

    Installation

  • Works with Next.js, Vite, CRA—doesn't matter. One command:

Terminal
npm install zustand
# or
yarn add zustand
# or pnpm
pnpm add zustand

If you're using TypeScript, you're good to go—no extra types needed.

Best React App File Structure + Zustand Integration

I always recommend a feature-based structure for any size project. Keep Zustand stores either in a dedicated folder or inside their feature for maximum clarity.

📁 Click to expand folders

Tree View

src
main.tsx
App.tsx

Why this works:

Feature-specific state lives next to its components → easy maintenance Truly global stuff (theme, modals) gets its own stores/ folder Scales beautifully as the app grows

How to Use Zustand

Creating a basic store:

TypeScript
src/stores/uiStore.ts
import { create } from "zustand";

type UIStore = {
  isSidebarOpen: boolean;
  toggleSidebar: () => void;
};

export const useUIStore = create<UIStore>((set) => ({
  isSidebarOpen: false,
  toggleSidebar: () =>
    set((state) => ({ isSidebarOpen: !state.isSidebarOpen })),
}));

Using it in a component:

React
tsx
import { useUIStore } from "@/stores/uiStore";

function SidebarButton() {
  const { isSidebarOpen, toggleSidebar } = useUIStore();

  return (
    <button onClick={toggleSidebar}>
      {isSidebarOpen ? "Close" : "Open"} Sidebar
    </button>
  );
}

Selective subscription (great for performance):

React
tsx
const isSidebarOpen = useUIStore((state) => state.isSidebarOpen);
// Re-renders only when this value changes

Persisting to localStorage (middleware example):

React
TypeScript
import { create } from "zustand";
import { persist } from "zustand/middleware";

export const useCartStore = create(
  persist(
    (set) => ({
      items: [],
      addItem: (item) => set((state) => ({ items: [...state.items, item] })),
    }),
    {
      name: "cart-storage", // localStorage key
    },
  ),
);

That's the core. Need more? Devtools, immer, or other middlewares are just a plug-in away.

Conclusion

Buddy, here's the bottom line: I pick Zustand because it hits the sweet spot. Context API struggles with performance and organization in larger apps. Redux drowns you in boilerplate and has a steep learning curve. MobX is powerful but mutation tracking can get messy. Zustand gives you simplicity without sacrificing power—minimal code, great performance, flawless TypeScript, and total flexibility. Even for small-to-medium projects, it future-proofs your app with zero migration pain later. If you want "less code, more done," Zustand is the way. Hit me up if you have questions—happy to help!

Check out GitHub
Last updated: --