Zustand for lightweight state management

Maya Patel Jan 2026
2 tabs
import { create } from 'zustand'
import { persist } from 'zustand/middleware'

interface FilterState {
  search: string
  category: string | null
  sortBy: 'recent' | 'popular' | 'oldest'
  setSearch: (search: string) => void
  setCategory: (category: string | null) => void
  setSortBy: (sortBy: FilterState['sortBy']) => void
  resetFilters: () => void
}

const initialState = {
  search: '',
  category: null,
  sortBy: 'recent' as const,
}

export const useFilterStore = create<FilterState>()(
  persist(
    (set) => ({
      ...initialState,

      setSearch: (search) => set({ search }),
      setCategory: (category) => set({ category }),
      setSortBy: (sortBy) => set({ sortBy }),
      resetFilters: () => set(initialState),
    }),
    {
      name: 'filter-storage',
    }
  )
)
2 files · typescript Explain with highlit

Zustand provides a minimalist alternative to Redux with less boilerplate and better TypeScript support. I create stores with create that hold state and actions. Unlike Context, Zustand doesn't cause unnecessary re-renders—components only update when their selected state changes. Stores can be sliced into modules for better organization in large apps. Middleware like persist saves state to localStorage automatically. Zustand works great for client state that's too complex for useState but doesn't warrant Redux's ceremony. I use it for filters, UI preferences, or shopping carts. The devtools extension provides time-travel debugging similar to Redux. For most apps, Zustand hits the sweet spot between simplicity and power.