Skip to content

Commit 8e4e82c

Browse files
authored
Don't recommend memoizing selectors (#1707)
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 (pmndrs/zustand#550) and this section may have been left behind as an oversight. - pmndrs/zustand#658 - pmndrs/zustand#971
1 parent ad88eb7 commit 8e4e82c

1 file changed

Lines changed: 0 additions & 23 deletions

File tree

docs/recipes/recipes.mdx

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -65,29 +65,6 @@ const currentBear = useCredentialsStore((state) => state.currentBear)
6565
const bear = useBearStore((state) => state.bears[currentBear])
6666
```
6767

68-
## Memoizing selectors
69-
70-
It is generally recommended to memoize selectors with `useCallback`.
71-
This will prevent unnecessary computations each render.
72-
It also allows React to optimize performance in concurrent mode.
73-
74-
```jsx
75-
const fruit = useStore(useCallback((state) => state.fruits[id], [id]))
76-
```
77-
78-
If a selector doesn't depend on scope,
79-
you can define it outside the render function
80-
to obtain a fixed reference without `useCallback`.
81-
82-
```jsx
83-
const selector = (state) => state.berries
84-
85-
function Component() {
86-
const berries = useStore(selector)
87-
// ...
88-
}
89-
```
90-
9168
## Overwriting state
9269

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

0 commit comments

Comments
 (0)