Skip to content

Commit

Permalink
Update Transient in recipes to match Readme (#1764)
Browse files Browse the repository at this point in the history
* Update Transient in recipes to match Readme



https://github.com/pmndrs/zustand#transient-updates-for-often-occurring-state-changes

* Update docs/recipes/recipes.mdx

Co-authored-by: Daishi Kato <[email protected]>

* update to useScratchStore

---------

Co-authored-by: Daishi Kato <[email protected]>
  • Loading branch information
timarney and dai-shi authored Apr 24, 2023
1 parent da45038 commit 513b222
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions docs/recipes/recipes.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -201,15 +201,14 @@ This can make a [drastic](https://codesandbox.io/s/peaceful-johnson-txtws)
performance impact, when you are allowed to mutate the view directly.

```jsx
const useStore = create(set => ({ scratches: 0, ... }))
const useScratchStore = create(set => ({ scratches: 0, ... }))

function Component() {
// Fetch initial state
const scratchRef = useRef(useStore.getState().scratches)
const scratchRef = useRef(useScratchStore.getState().scratches)
// Connect to the store on mount, disconnect on unmount, catch state-changes in a reference
useEffect(() => useStore.subscribe(
scratches => (scratchRef.current = scratches),
state => state.scratches
useEffect(() => useScratchStore.subscribe(
(state) => (scratchRef.current = state.scratches)
), [])
// ...
}
Expand Down

0 comments on commit 513b222

Please sign in to comment.