mirror of
https://github.com/pmndrs/zustand.git
synced 2025-12-08 19:45:52 +00:00
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:
parent
5493959646
commit
0058a03b78
@ -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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user