← BACK TO PORTFOLIO

/WRITING · BUILDING WEB APPS

Zustand v5 in the React Compiler era: selectors, slices and stores per request

Small apps are easy to keep tidy. As they grow, the shared information every screen relies on can turn into a slow, tangled mess. Here are the simple habits I use with Zustand to keep apps fast and easy to change as they grow.

January 26, 20263 MIN READ
Cover illustration: a Zustand store with four fields; thin selector lines connect each component only to the field it reads, so a change to one field lights up exactly one component.

Most apps start small and tidy. Then features pile up: a basket, a user menu, filters, notifications, settings. Each needs to share a little information with other parts of the app, and before long there is one big shared store that everything depends on. Change one thing and three unrelated screens slow down or break.

I use a small tool called Zustand to hold this kind of shared information in React apps. It is popular precisely because it is simple. React has also gained a new helper, the React Compiler, that automatically speeds up many apps. Together they remove a lot of old busywork, but a few habits still matter. This post covers them in plain terms.

The problem: shared information that grows without a plan

Three problems show up as apps grow:

  • Every screen redraws when anything changes. If a component reads the whole store, adding an item to the basket can make the header, the sidebar and the footer all redraw.
  • Everything lives in one giant file. Nobody knows which part of the app owns which piece, so changes become risky.
  • On websites that are built on the server, one visitor's information can leak to another if the shared store is set up carelessly.
Three Zustand selector styles: whole-store re-renders everything, a new object each time can loop in v5, and atomic selectors re-render only the cart badge.
Fig. 01 — When a screen asks for only what it needs, it only redraws when that changes. Asking for everything means redrawing on every change.

The big idea: ask for only what you need, and split by feature

The habits that fix these problems are simple:

  • Each part of the screen asks for exactly the pieces it uses, nothing more. Then it only redraws when those pieces change.
  • The store is split into small sections by feature, such as basket, user and filters, each with its own file and owner.
  • On server-built websites, each visitor gets their own fresh store, so nothing is ever shared between people.

How to do it

1. Ask for single values

Instead of reading the whole store, each component reads one or two values, like the number of items in the basket. Tools like the React Compiler help, but they cannot fix a component that asks for everything.

2. Split the store into slices

I give each feature its own slice of the store with a clear name and a short list of actions, such as "add to basket" or "clear filters". New team members can find what they need, and changes stay contained.

3. Create one store per visitor on server-built pages

With frameworks like Next.js, pages are often prepared on the server. A single shared store on the server would be shared by every visitor at the same time. Creating a fresh store for each visit prevents that entirely.

A module-level store shared across server requests leaks data between users; a store created per request in a provider keeps each request isolated.
Fig. 02 — One shared store on the server is used by every visitor at once; a store created per visit belongs to one person only.

4. Keep server data out of it

Products, orders and balances belong with the server and a tool built to keep them fresh. The shared store is for small things only the browser cares about. That single rule keeps the store small for the life of the app.

Takeaway

As apps grow, shared information either stays tidy or slowly becomes the thing everyone is afraid to touch. Let each screen ask only for what it needs, split the store by feature, give each visitor their own store on server-built pages, and keep server data elsewhere. The app stays fast, and changes stay safe.

References

/OCCASIONAL_NOTES

Useful notes, when there’s something worth sharing.

A low-volume digest on product systems, interfaces, and technical work. Your email stays in Taiwo’s private portfolio list—no third-party tracking.