Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't recommend memoizing selectors #1707

Merged
merged 1 commit into from
Mar 22, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 0 additions & 23 deletions docs/recipes/recipes.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -65,29 +65,6 @@ const currentBear = useCredentialsStore((state) => state.currentBear)
const bear = useBearStore((state) => state.bears[currentBear])
```

## Memoizing selectors

It is generally recommended to memoize selectors with `useCallback`.
This will prevent unnecessary computations each render.
It also allows React to optimize performance in concurrent mode.

```jsx
const fruit = useStore(useCallback((state) => state.fruits[id], [id]))
```

If a selector doesn't depend on scope,
you can define it outside the render function
to obtain a fixed reference without `useCallback`.

```jsx
const selector = (state) => state.berries

function Component() {
const berries = useStore(selector)
// ...
}
```

## Overwriting state

The `set` function has a second argument, `false` by default.
Expand Down