Skip to content

Commit

Permalink
Don't recommend memoizing selectors
Browse files Browse the repository at this point in the history
As per the discussions below, memoizing selectors does not improve performance
significantly as of v4. Thus we should avoid recommending selector memoization.
Note that the same paragraph was removed from the README in PR #550
(#550)
and this section may have been left behind as an oversight.

- #658
- #971
  • Loading branch information
pastelmind authored Mar 22, 2023
1 parent 1dd85fe commit e7f2b46
Showing 1 changed file with 0 additions and 23 deletions.
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

0 comments on commit e7f2b46

Please sign in to comment.