fix(docs): Do not recommend deprecated useStore + equalityFn on initialize 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>
This commit is contained in:
Michel Sabchuk 2023-08-16 20:16:19 -03:00 committed by GitHub
parent 5493959646
commit 0058a03b78
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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)
}
```
@ -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