Sub topic 1
Why Zustand and when to reach for it
Zustand is a small state management library for React. It keeps shared state in a store that lives outside the component tree. Components subscribe to the parts of the store they need. This chapter explains the problem Zustand solves and helps you decide when to use it.
Prerequisites
This tutorial assumes you already know:
- React function components and hooks such as
useStateanduseEffect. - Basic JavaScript or TypeScript: modules, arrow functions, and object spread.
- The idea of shared state and prop drilling.
- Immutable updates, meaning you build a new object instead of changing the old one.
The problem: shared state
useState works well for state that belongs to one component. Trouble starts when distant components need the same data, such as a signed-in user, a shopping cart, or a theme.
You then have two common options:
- Lift state up and pass it down as props. This leads to prop drilling, where components in the middle forward props they do not use.
- Use React context. This removes the drilling. But when a context value changes, every component that reads that context re-renders, even if it only needs one field of the value.
Zustand takes a third route. State lives in a store, and a component subscribes to a slice of it with a selector function. A component re-renders only when the value its selector returns changes.
What Zustand gives you
- A small API. A store is one function call. There are no providers, reducers, or action types to set up.
- Hooks as the interface. The store you create is itself a React hook.
- Selective re-rendering. Selectors let you control exactly what each component listens to.
- Use outside React. You can read and update the store from plain JavaScript, such as a utility module or a test.
- Optional middleware. Persistence, Redux DevTools, and immer support are add-ons you opt into.
How it compares
| Tool | Best for | Cost |
|---|---|---|
| `useState` | State owned by one component | None, but sharing needs props |
| Context | Rarely changing values such as theme or locale | Whole-context re-renders |
| Zustand | Shared client state that changes often | One extra dependency |
| Redux Toolkit | Large teams that want strict structure | More concepts and boilerplate |
When to reach for it
Zustand fits when:
- Several unrelated components need the same client-side state.
- That state changes often, so context re-renders would hurt.
- You want something lighter than Redux.
It is a poor fit for server data that needs caching, refetching, and invalidation. A dedicated data-fetching library handles that better. It is also unnecessary for state that only one component or a small subtree uses. Keep that in useState.
The next chapter builds your first store with create.
Exercise
A shopping cart is read by a header badge, a product page, and a checkout page. These components sit in different parts of the tree and the cart changes often. Why might Zustand be a better choice than context here?
Show answer
With context, every component reading the cart context re-renders on every cart change, even if it only needs one field. With Zustand, each component uses a selector and re-renders only when the value it selected changes. Zustand also needs no provider wrapping the tree.