Skip to content

Commit

Permalink
fix(docs): Do not recommend deprecated useStore + equalityFn on initi…
Browse files Browse the repository at this point in the history
…alize with props docs (#1997)

* Do not recommend deprecated useStore + equalityFn

* Show alternative without equality function

* Adjust content to be more clear

* Update docs/guides/initialize-state-with-props.md

Co-authored-by: Blazej Sewera <code@sewera.dev>

* Apply prettier

---------

Co-authored-by: Blazej Sewera <code@sewera.dev>
  • Loading branch information
michelts and sewera authored Aug 16, 2023
1 parent 5493959 commit 0058a03
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions docs/guides/initialize-state-with-props.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,10 @@ function BearProvider({ children, ...props }: BearProviderProps) {
import { useContext } from 'react'
import { useStore } from 'zustand'

function useBearContext<T>(
selector: (state: BearState) => T,
equalityFn?: (left: T, right: T) => boolean
): T {
function useBearContext<T>(selector: (state: BearState) => T): T {
const store = useContext(BearContext)
if (!store) throw new Error('Missing BearContext.Provider in the tree')
return useStore(store, selector, equalityFn)
return useStore(store, selector)
}
```

Expand All @@ -129,6 +126,23 @@ function CommonConsumer() {
}
```

### Optionally allow using a custom equality function

```tsx
// Allow custom equality function by using useStoreWithEqualityFn instead of useStore
import { useContext } from 'react'
import { useStoreWithEqualityFn } from 'zustand/traditional'

function useBearContext<T>(
selector: (state: BearState) => T,
equalityFn?: (left: T, right: T) => boolean
): T {
const store = useContext(BearContext)
if (!store) throw new Error('Missing BearContext.Provider in the tree')
return useStoreWithEqualityFn(store, selector, equalityFn)
}
```

### Complete example

```tsx
Expand Down

1 comment on commit 0058a03

@vercel
Copy link

@vercel vercel bot commented on 0058a03 Aug 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.