ZustandState ManagementError Fix2026 Updated

Zustand State Not Updating— Component Not Re-rendering [Fixed 2026]

Also covers: direct mutation · selectors · shallow equality · nested state · immer middleware

January 20266 min readZustand v4/v5 · React · TypeScript

⚡ The Problem

Error
// Direct mutation — state changes but component never re-renders
const useStore = create((set, get) => ({
  user: { name: "Alice" },
  updateName: (name) => {
    get().user.name = name   // ❌ mutating state directly — no re-render
  },
}))

✅ Fix — always use set() and return new objects

Correct — spread to create new object reference
updateName: (name) =>
  set(state => ({ user: { ...state.user, name } }))  // ✅ new object → re-renders

Why Zustand State Doesn't Trigger a Re-render

Zustand detects state changes by reference equality — it compares the previous state reference to the new one. If you mutate the existing state object in-place, the reference stays the same, Zustand sees no change, and components don't re-render. You must always return a new object (or value) from set().

1

Never Mutate State Directly — Always Use set()

Root cause — in-place mutation bypasses Zustand change detection
2 min
Direct mutation vs correct set() pattern
import { create } from "zustand"

interface CartStore {
  items: CartItem[]
  addItem: (item: CartItem) => void
  removeItem: (id: string) => void
  clearCart: () => void
}

const useCartStore = create<CartStore>((set, get) => ({
  items: [],

  // ❌ Mutating the array directly — no re-render
  addItem: (item) => {
    get().items.push(item)   // in-place mutation — reference unchanged
  },

  // ✅ Return new array — Zustand detects change, triggers re-render
  addItem: (item) => set(state => ({
    items: [...state.items, item]
  })),

  // ✅ Filter returns new array
  removeItem: (id) => set(state => ({
    items: state.items.filter(item => item.id !== id)
  })),

  // ✅ Replace with empty array — new reference
  clearCart: () => set({ items: [] }),
}))

Zustand's set() automatically merges top-level state — you only need to return the changed keys, not the entire state. set({ items: [] }) is equivalent to set(state => ({ ...state, items: [] })).

2

Use a Selector to Subscribe to Specific State Slices

Component re-renders on every store change — select only what you need
2 min
Selector patterns — subscribe to minimum state
const useStore = create<Store>()((set) => ({
  count: 0,
  user: { name: "Alice", role: "admin" },
  theme: "dark",
  increment: () => set(s => ({ count: s.count + 1 })),
}))

// ❌ No selector — re-renders on ANY store change (count, user, theme, etc.)
function Counter() {
  const store = useStore()   // subscribes to entire store
  return <button onClick={store.increment}>{store.count}</button>
}

// ✅ Selector — only re-renders when count changes
function Counter() {
  const count = useStore(s => s.count)
  const increment = useStore(s => s.increment)
  return <button onClick={increment}>{count}</button>
}

// ✅ Combine selectors for multiple values
function UserBadge() {
  const name = useStore(s => s.user.name)
  const role = useStore(s => s.user.role)
  return <span>{name} ({role})</span>
}
3

Use shallow for Object and Array Selectors

Selector returns object — component re-renders every time despite same values
3 min

When your selector returns an object or array, Zustand compares the reference — a new object with identical contents still triggers a re-render. Use shallow to compare one level deep by value.

shallow equality for object selectors
import { useShallow } from "zustand/react/shallow"

const useStore = create<Store>()((set) => ({
  user: { name: "Alice", role: "admin", avatar: null },
  settings: { theme: "dark", lang: "en" },
}))

// ❌ Returns new object on every render — re-renders even if values unchanged
function UserInfo() {
  const { name, role } = useStore(s => ({ name: s.user.name, role: s.user.role }))
  // New { name, role } object created every render → always different reference
}

// ✅ useShallow — compares values one level deep, not the object reference
function UserInfo() {
  const { name, role } = useStore(
    useShallow(s => ({ name: s.user.name, role: s.user.role }))
  )
  // Only re-renders when name or role actually changes
}

// ✅ Alternative — use separate selectors per primitive value
function UserInfo() {
  const name = useStore(s => s.user.name)   // string — stable comparison
  const role = useStore(s => s.user.role)   // string — stable comparison
  // No shallow needed — primitives use Object.is which works correctly
}
4

Update Nested State Correctly

Nested object update — top level updates but nested values don't reflect
3 min

Zustand merges at the top level only. For nested objects, you must manually spread each level to create new references at every depth that changed.

Nested state update patterns
interface Store {
  user: {
    profile: { name: string; bio: string }
    preferences: { theme: string; notifications: boolean }
  }
}

const useStore = create<Store>()((set) => ({
  user: {
    profile: { name: "Alice", bio: "" },
    preferences: { theme: "dark", notifications: true },
  },

  // ❌ Wrong — only spreads top level, user.profile reference unchanged
  updateName: (name: string) => set(state => ({
    user: { name }   // replaces entire user — loses profile and preferences
  })),

  // ✅ Spread each nested level that changes
  updateName: (name: string) => set(state => ({
    user: {
      ...state.user,             // keep preferences unchanged
      profile: {
        ...state.user.profile,   // keep bio unchanged
        name,                    // update only name
      },
    },
  })),

  // ✅ Toggle notifications — spread every level
  toggleNotifications: () => set(state => ({
    user: {
      ...state.user,
      preferences: {
        ...state.user.preferences,
        notifications: !state.user.preferences.notifications,
      },
    },
  })),
}))

Deeply nested manual spreading becomes verbose and error-prone quickly. If you have more than 2 levels of nesting, switch to the immer middleware (Fix 5) which handles this automatically.

5

Use immer Middleware for Complex Nested Updates

3+ levels of nesting — immer makes mutations readable
4 min
immer middleware — write mutations, get immutable updates
import { create } from "zustand"
import { immer } from "zustand/middleware/immer"

interface Store {
  cart: {
    items: { id: string; qty: number; price: number }[]
    coupon: string | null
    shipping: { method: string; cost: number }
  }
  updateQty: (id: string, qty: number) => void
  applyCoupon: (code: string) => void
  setShipping: (method: string, cost: number) => void
}

// ✅ immer middleware — write as if mutating, immer handles immutability
const useCartStore = create<Store>()(
  immer((set) => ({
    cart: {
      items: [],
      coupon: null,
      shipping: { method: "standard", cost: 5.99 },
    },

    // Write like direct mutation — immer converts to immutable update
    updateQty: (id, qty) => set(state => {
      const item = state.cart.items.find(i => i.id === id)
      if (item) item.qty = qty   // looks like mutation — immer handles it
    }),

    applyCoupon: (code) => set(state => {
      state.cart.coupon = code   // direct assignment — works with immer
    }),

    setShipping: (method, cost) => set(state => {
      state.cart.shipping.method = method
      state.cart.shipping.cost = cost
    }),
  }))
)

// Compare without immer — same updateQty:
// updateQty: (id, qty) => set(state => ({
//   cart: {
//     ...state.cart,
//     items: state.cart.items.map(i => i.id === id ? { ...i, qty } : i)
//   }
// }))

Prevention

Frequently Asked Questions

Why is my Zustand component not re-rendering after state changes?+
The most common cause is direct state mutation — modifying the state object in-place instead of calling set() with a new object. Zustand uses referential equality to detect changes: if the state object reference hasn't changed, no re-render is triggered. Always return a new object from set().
What is a Zustand selector and why should I use one?+
A selector is the function you pass to useStore — it extracts the specific piece of state your component needs. Without a selector, your component re-renders on every single state change in the store. With a selector, it only re-renders when the selected value changes.
When do I need to use shallow in Zustand?+
Use shallow when your selector returns an object or array. Zustand uses Object.is for comparison — a new object { a: 1 } !== { a: 1 } even if contents are identical. shallow does a one-level property comparison, so components only re-render when the actual values change, not just the reference.
How do I update nested state in Zustand?+
Spread each nested level to create new object references. Zustand's set() merges the top level automatically, but nested objects must be spread manually: set(state => ({ user: { ...state.user, name: 'Alice' } })). For deeply nested state, use the immer middleware to write mutations that are internally converted to immutable updates.
Can I use Zustand without a selector?+
Yes, but it causes over-rendering. useStore() without a selector returns the whole state and re-renders your component on every state change anywhere in the store. Always use a selector: const count = useStore(s => s.count) to subscribe only to what your component needs.
What is the immer middleware in Zustand?+
The immer middleware lets you write state mutations as if they were direct mutations (e.g. state.user.name = 'Alice') and automatically converts them to proper immutable updates behind the scenes. It is particularly useful for deeply nested state where manual spreading becomes cumbersome.

Need Expert Help?

We Build React Apps with Zustand

Softplix engineers architect Zustand stores with selectors, immer, and persistence for production React apps. Let us help.

Talk to an Engineer