# Selectors & useShallow

> Prevent unnecessary re-renders by selecting only the data your component needs.

Subject: State Management | Lesson 6 of 22 | 6 min read
Interactive lesson: https://codewithanimation.in/state-management/zustand-selectors-and-shallow
Previous: [Actions & State Mutations](https://codewithanimation.in/state-management/zustand-actions.md) | Next: [The Slices Pattern](https://codewithanimation.in/state-management/zustand-slices-pattern.md)

## What are Selectors?

A selector is a function you pass to your Zustand hook to pick out a specific piece of state. By default, Zustand uses strict equality (`===`) to compare the return value of your selector with its previous value. If it changes, your component re-renders.

`const bears = useStore((state) => state.bears);`

## The Object Literal Problem

If you need multiple values from the store, you might be tempted to return an object literal from your selector:

`const { bears, fish } = useStore((state) => ({ bears: state.bears, fish: state.fish }));`

Because JavaScript compares objects by reference, this creates a **new object** every time the store updates. This means your component will re-render every single time *anything* in the store changes, defeating Zustand's performance benefits.

## Fixing it with `useShallow`

Zustand provides a `useShallow` helper to fix this. It tells Zustand to shallow-compare the keys of the object you return, rather than strictly comparing the object reference.

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

const { bears, fish } = useStore(
  useShallow((state) => ({ bears: state.bears, fish: state.fish }))
);
```

Now, the component will only re-render if `bears` or `fish` actually changes.

## Common mistake: Selector Boilerplate vs Performance

Some developers prefer writing multiple hooks to avoid `useShallow` entirely:
`const bears = useStore((state) => state.bears);`
`const fish = useStore((state) => state.fish);`
This works perfectly and is highly performant! However, if you are grabbing 5 or 6 items from a store, `useShallow` with object destructuring keeps your code much cleaner.

## Tip: Don't select everything

Never do this: `const state = useStore()`. If you call the hook without a selector, your component will re-render on literally every state change. Always be specific about what you are subscribing to.

## Try it: Re-render Visualization

In this interactive lab, use the toolbar to update different parts of the store. Watch how components using proper selectors ignore updates they don't care about, while poorly configured components re-render pointlessly.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/state-management/zustand-selectors-and-shallow

## Check yourself

### Question 1

Why does returning an object from a Zustand selector cause unnecessary re-renders?

- A. Zustand doesn't support returning objects from selectors.
- B. Because a new object reference is created on every state update, failing strict equality checks.
- C. Because React Context forces the update regardless of the selector.

Correct answer: B

By default, Zustand uses strict equality (`===`). Since `{} === {}` is false in JavaScript, a newly created object always triggers a re-render unless you use `useShallow`.

- Why not A: It supports it, but you need to be careful with reference equality.
- Why not C: Zustand doesn't use Context.

## Remember this

- Selectors allow components to subscribe to specific slices of state.
- Returning object literals from selectors breaks strict equality checks.
- Use `useShallow` from `zustand/react/shallow` when returning objects.
- Calling `useStore()` without a selector subscribes to all state changes.
