Zustand State Not Updating— Component Not Re-rendering [Fixed 2026]
Also covers: direct mutation · selectors · shallow equality · nested state · immer middleware
⚡ The Problem
// 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
updateName: (name) =>
set(state => ({ user: { ...state.user, name } })) // ✅ new object → re-rendersWhy 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().
Never Mutate State Directly — Always Use set()
Root cause — in-place mutation bypasses Zustand change detectionimport { 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: [] })).
Use a Selector to Subscribe to Specific State Slices
Component re-renders on every store change — select only what you needconst 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>
}Use shallow for Object and Array Selectors
Selector returns object — component re-renders every time despite same valuesWhen 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.
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
}Update Nested State Correctly
Nested object update — top level updates but nested values don't reflectZustand merges at the top level only. For nested objects, you must manually spread each level to create new references at every depth that changed.
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.
Use immer Middleware for Complex Nested Updates
3+ levels of nesting — immer makes mutations readableimport { 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
- Never mutate state directly — always call set() and return new objects or values
- Always use a selector in useStore(s => s.value) — never subscribe to the whole store
- Use useShallow when your selector returns an object or array to avoid unnecessary re-renders
- Spread every nested level that changes — Zustand only auto-merges the top level
- Use immer middleware for stores with 3+ levels of nesting — cleaner and less error-prone
- Install Zustand DevTools middleware in development to inspect state changes in real time
Frequently Asked Questions
Why is my Zustand component not re-rendering after state changes?+−
What is a Zustand selector and why should I use one?+−
When do I need to use shallow in Zustand?+−
How do I update nested state in Zustand?+−
Can I use Zustand without a selector?+−
What is the immer middleware in Zustand?+−
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