Consuming Store State with Hooks
What you'll learn: How to read values from your Zustand store and make your components automatically re-render when that specific data changes.
The Hook Pattern
With Zustand, you consume state by calling your store as a hook inside components. This feels natural if you're used to useState or useContext — but with a key difference: you can select exactly which piece of state you need.
When you call your store hook, you pass a selector function that picks out the slice of state you care about. React will only re-render your component when that specific slice changes, not when any part of the store updates.
How It Works
// Your store (already created)
const useStore = create((set) => ({
count: 0,
user: { name: 'Alice' },
increment: () => set((state) => ({ count: state.count + 1 }))
}));
// Component that reads count
function Counter() {
const count = useStore((state) => state.count);
return <div>{count}</div>;
}
// Component that reads user
function UserName() {
const userName = useStore((state) => state.user.name);
return <div>{userName}</div>;
}
When increment runs and count changes, only Counter re-renders. UserName stays untouched because state.user.name didn't change.
The Selector is Key
Think of the selector function like a filter: you're telling React "only wake me up when this changes." Without selectors, every component would re-render on every state change — defeating the purpose of moving beyond Context.
Key Takeaway: Call your Zustand store as a hook with a selector function to read state — components automatically re-render only when the selected slice changes, giving you precise performance control without Providers.