-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add useShallow See - #1937 - #1937 (reply in thread) - #1937 (reply in thread) * chore(useShallow): improve unit tests * chore(useShallow): PR feedback #2090 (comment) * fix(useShallow): tests not working on test_matrix (cjs, production, CI-MATRIX-NOSKIP) * chore(useShallow): fix eslint warning issue (unused import) * refactor(useShallow): simplify tests * docs(useShallow): add guide * fix(useShallow): prettier:ci error https://github.com/pmndrs/zustand/actions/runs/6369420511/job/17289749161?pr=2090 * docs(useShallow): update readme * docs(useShallow): remove obsolete line from readme Co-authored-by: Daishi Kato <dai-shi@users.noreply.github.com> * doc(useShallow): PR feedback #2090 (comment) * docs(useShallow): small improvements of the useShallow guide --------- Co-authored-by: Daishi Kato <dai-shi@users.noreply.github.com>
- Loading branch information
1 parent
1e846b3
commit 3cbd468
Showing
4 changed files
with
245 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
--- | ||
title: Prevent rerenders with useShallow | ||
nav: 16 | ||
--- | ||
|
||
When you need to subscribe to a computed state from a store, the recommended way is to | ||
use a selector. | ||
|
||
The computed selector will cause a rererender if the output has changed according to [Object.is](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is?retiredLocale=it). | ||
|
||
In this case you might want to use `useShallow` to avoid a rerender if the computed value is always shallow | ||
equal the previous one. | ||
|
||
## Example | ||
|
||
We have a store that associates to each bear a meal and we want to render their names. | ||
|
||
```js | ||
import { create } from 'zustand' | ||
|
||
const useMeals = create(() => ({ | ||
papaBear: 'large porridge-pot', | ||
mamaBear: 'middle-size porridge pot', | ||
littleBear: 'A little, small, wee pot', | ||
})) | ||
|
||
export const BearNames = () => { | ||
const names = useMeals((state) => Object.keys(state)) | ||
|
||
return <div>{names.join(', ')}</div> | ||
} | ||
``` | ||
|
||
Now papa bear wants a pizza instead: | ||
|
||
```js | ||
useMeals.setState({ | ||
papaBear: 'a large pizza', | ||
}) | ||
``` | ||
|
||
This change causes `BearNames` rerenders even tho the actual output of `names` has not changed according to shallow equal. | ||
|
||
We can fix that using `useShallow`! | ||
|
||
```js | ||
import { create } from 'zustand' | ||
import { useShallow } from 'zustand/shallow' | ||
|
||
const useMeals = create(() => ({ | ||
papaBear: 'large porridge-pot', | ||
mamaBear: 'middle-size porridge pot', | ||
littleBear: 'A little, small, wee pot', | ||
})) | ||
|
||
export const BearNames = () => { | ||
const names = useMeals(useShallow((state) => Object.keys(state))) | ||
|
||
return <div>{names.join(', ')}</div> | ||
} | ||
``` | ||
|
||
Now they can all order other meals without causing unnecessary rerenders of our `BearNames` component. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3cbd468
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
zustand-demo – ./
zustand-demo-git-main-pmndrs.vercel.app
zustand-demo.vercel.app
zustand-demo.pmnd.rs
zustand-demo-pmndrs.vercel.app