Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(docs): Do not recommend deprecated useStore + equalityFn on initialize with props docs #1997

Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions docs/guides/initialize-state-with-props.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,10 @@ import { useStore } from 'zustand'

function useBearContext<T>(
selector: (state: BearState) => T,
michelts marked this conversation as resolved.
Show resolved Hide resolved
equalityFn?: (left: T, right: T) => boolean
): 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 +128,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