Zustand and minimal state stores
Zustand's create call and selector hooks as a minimal external store, and why it avoids Context's re-render-everything problem.
Zustand and minimal state stores
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- Zustand's create() returns a hook, not a provider — there's no <Provider> wrapping your tree, and any component can import and call that hook directly to read or update the store.
- A component that calls the store's hook with a selector, like useStore((s) => s.count), only re-renders when the specific slice that selector returns actually changes, not whenever anything in the store changes.
- That selective re-rendering is the direct fix for Context's limitation: Context has one value and re-renders every consumer on any change, while Zustand lets each consumer subscribe to only the slice it cares about.
- State and the functions that update it live together in the same create() call, so a store is a single, colocated definition of both the global client state and its update logic.
- Zustand's store lives outside React entirely, as a plain JavaScript object with a subscription mechanism; React components just happen to be able to subscribe to it through the hook.
A store without a provider
The previous lesson identified the gap: Context re-renders every consumer whenever its value changes, which makes it a poor fit for global state that updates often. Zustand closes that gap with a different shape entirely — no provider, no context, just a hook backed by a plain store object:
import { create } from "zustand";
const useCartStore = create((set) => ({
items: [],
addItem: (item) =>
set((state) => ({ items: [...state.items, item] })),
removeItem: (id) =>
set((state) => ({ items: state.items.filter((i) => i.id !== id) })),
}));
create() returns useCartStore, a hook. There’s no <CartProvider> anywhere — any component in the app can import { useCartStore } and call it directly, and it will read from the exact same store every other component reads from, the same way a module-level variable would be shared, except reactive.
function AddToCartButton({ product }) {
const addItem = useCartStore((s) => s.addItem);
return <button onClick={() => addItem(product)}>Add to cart</button>;
}
function CartBadge() {
const itemCount = useCartStore((s) => s.items.length);
return <span className="badge">{itemCount}</span>;
}
Selectors are where the performance win lives
Look closely at those two components. AddToCartButton selects s.addItem — a function reference that never changes — so it essentially never re-renders because of this store. CartBadge selects s.items.length, so it re-renders only when the item count itself changes, not when, say, a lastViewedProduct field elsewhere in the same store updates.
This is the direct answer to Context’s limitation. A Context consumer has no way to say “I only want this slice” — it gets the whole value object and re-renders on any change to it. A Zustand selector is exactly that missing subscription granularity: each component declares the narrow function of state it depends on, and Zustand only notifies that component when the result of that function actually changes between updates (compared with strict equality by default).
Contrast this with the Context version of the same cart, where a component reading only itemCount from a context value { items, addItem, removeItem } would re-render every time items changed in any way, including changes that don’t affect the count — or every time a sibling field in that same context value changed at all.
State and actions, colocated
Notice that addItem and removeItem are defined right inside create(), next to the state they modify. There’s no separate reducer function, no action-type constants, no dispatch — set is just a function that merges a partial update into the store, similar in spirit to setState, but callable from anywhere, not just inside a component.
const useAuthStore = create((set) => ({
user: null,
login: (user) => set({ user }),
logout: () => set({ user: null }),
}));
// Anywhere, including outside a component:
useAuthStore.getState().logout();
That last line is worth noting: because the store isn’t tied to React’s render cycle, you can read or update it from a non-component context too — an event handler attached outside React, a fetch interceptor, a test. That’s a consequence of the store being a plain object with a subscription layer, not a React-specific construct.
Where Zustand’s simplicity runs out
Zustand intentionally has no middleware ecosystem as elaborate as Redux’s, no time-travel debugging built in by default, and no enforced structure for large stores — you decide how to split state across multiple create() calls or combine them, with comparatively little guidance. For small-to-medium global state — a cart, a theme, an auth session — that minimalism is the whole appeal: there’s barely any boilerplate standing between “I need global state” and a working store. For a large app with many contributors and state so interrelated that strict structure and tooling start to pay for themselves, that’s where the next lesson’s tradeoff comes in.
What the video covers
Web Dev Simplified’s walkthrough covers Zustand’s basics around the 2:38 mark, best practices around 11:48, and more advanced store patterns from 14:55, framed explicitly against the built-in alternatives — Context among them — that this lesson contrasts it with. It’s a focused tutorial rather than a side-by-side re-render benchmark, so the concrete selector-versus-context re-render argument above is this lesson’s own addition; watch the video for the API shape and conventions, and use the explanation here for why those conventions avoid Context’s re-render cost.
Where this fits
The next lesson is Redux Toolkit for complex apps, which covers when a more structured, tooling-heavy store actually earns its keep over Zustand’s minimalism.
Check yourself
Why doesn't a Zustand store require wrapping the app in a <Provider>, the way Context does?
The store created by create() is a plain object living outside the React tree, with its own subscription mechanism, and the hook it returns reads from that object directly wherever it's called. There's no tree position the store needs to be provided from, so no provider component is needed.
A component calls useCartStore((s) => s.itemCount), and a different part of the same store, s.lastUpdatedAt, changes. Does this component re-render?
No. Because the component subscribed with a selector that only reads itemCount, Zustand only re-renders it when the value that selector returns changes. A change to a different field in the same store does not trigger a re-render for this component.
What is the core difference between how Context and Zustand handle a component that only cares about one slice of shared state?
Context delivers its single value to every consumer and re-renders all of them whenever that value changes, with no way to read just part of it. Zustand's selector-based hook lets each component specify exactly which slice it wants, so only components whose selected slice actually changed re-render.
In Zustand, where do the functions that update the store's state typically live?
Inside the same create() call that defines the state itself, so a single store definition contains both the data and the actions that modify it, rather than state and updater logic being split across separate files or hooks.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.