Zustand 5 and the selector that loops forever

Zustand 5 came out in October, and its release notes promise 'no new features' and a smooth migration. For most stores, that's true.

Zustand 5 came out in October, and its release notes promise “no new features” and a smooth migration. For most stores, that’s true. But there’s one pattern that worked fine in v4 and crashes in v5, and it’s a pattern almost every Zustand codebase has somewhere:

const { items, total } = useCart((state) => ({
  items: state.items,
  total: state.total,
}));

In v5, this component throws “Maximum update depth exceeded”, and React logs a warning that “the result of getSnapshot should be cached”. The release notes list it as the most frequently reported issue from the release candidates.

Why it loops

The selector returns a new object every time it runs. Two objects with the same contents are still different objects.

Zustand 4 used a shim package, use-sync-external-store/shim/with-selector, that remembered the selector’s result for a given state. As long as the store hadn’t changed, you got the same object back, even though your selector built a new one.

Zustand 5 drops that layer and uses React 18’s built-in useSyncExternalStore directly. React calls your selector to read the current value, compares it with the last one using Object.is, and sees a change every time, because there’s a new object every time. A change means a re-render, the re-render calls the selector again, and the loop never ends.

I ran the same component against both versions, with React 18.3. With 4.5.5 it rendered twice and worked. With 5.0.1 it hit React’s limit after about 50 renders and threw.

The fix: useShallow

Wrap the selector in useShallow, which compares the result’s top-level keys instead of the object itself:

import { useShallow } from 'zustand/react/shallow';

const { items, total } = useCart(
  useShallow((state) => ({ items: state.items, total: state.total })),
);

Now a new object with the same items and total counts as no change, and the component only re-renders when one of them actually changes.

The same applies to arrays, which are easier to miss:

// loops in v5: a new array on every call
const names = useCart((state) => state.items.map((i) => i.name));

// fine: compare the array's elements
const names = useCart(useShallow((state) => state.items.map((i) => i.name)));

Or don’t build objects in selectors

Often the cleaner fix is one selector per value:

const items = useCart((state) => state.items);
const total = useCart((state) => state.total);

Each selector returns something that already lives in the store, so the reference only changes when the value does. And for anything derived, like a filtered list, select the source and derive it in the component:

const items = useCart((state) => state.items);
const onSale = useMemo(() => items.filter((i) => i.onSale), [items]);

A good rule for v5: if a selector contains {, [, .map, .filter or a spread, it needs useShallow, or it should be split up.

What if you used a custom equality function?

In v4, you could pass an equality function as the second argument:

// v4
import { shallow } from 'zustand/shallow';
const { items, total } = useCart((s) => ({ items: s.items, total: s.total }), shallow);

create in v5 doesn’t accept that any more. If you have a lot of these and want the old behaviour back quickly, there’s a drop-in in zustand/traditional. It needs the use-sync-external-store package installed, since that’s no longer a dependency:

// npm install use-sync-external-store
import { createWithEqualityFn } from 'zustand/traditional';
import { shallow } from 'zustand/shallow';

const useCart = createWithEqualityFn((set) => ({ /* ... */ }), shallow);

I’d treat that as a stepping stone. useShallow at the call sites is the v5 way.

The other changes

The rest are small, and mostly fail loudly:

  • No default exports. import create from ‘zustand’ becomes import { create } from ‘zustand’.
  • React 18 is the minimum. That’s what made the switch to the built-in useSyncExternalStore possible.
  • persist doesn’t write the initial state on creation. In v4, creating a persisted store saved its initial state to storage straight away. v5 (and 4.5.5) only saves when the state changes. If you relied on the key existing in localStorage from the first load, set the state explicitly after creating the store.

How I’d upgrade

Update to the latest v4 first, which prints deprecation warnings for most of this without breaking anything. Then search for selectors that return objects or arrays, wrap them in useShallow or split them, and only then bump to v5. The loop is easy to fix once you know what it is, and a lot less fun to discover in a component that only renders on one rarely visited page.