From 0058a03b78bf628a27fb3cb4b6ebe3e6263dd0a0 Mon Sep 17 00:00:00 2001 From: Michel Sabchuk Date: Wed, 16 Aug 2023 20:16:19 -0300 Subject: [PATCH] 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 * Apply prettier --------- Co-authored-by: Blazej Sewera --- docs/guides/initialize-state-with-props.md | 24 +++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/docs/guides/initialize-state-with-props.md b/docs/guides/initialize-state-with-props.md index c1397ab356..8bb1754b55 100644 --- a/docs/guides/initialize-state-with-props.md +++ b/docs/guides/initialize-state-with-props.md @@ -105,13 +105,10 @@ function BearProvider({ children, ...props }: BearProviderProps) { import { useContext } from 'react' import { useStore } from 'zustand' -function useBearContext( - selector: (state: BearState) => T, - equalityFn?: (left: T, right: T) => boolean -): T { +function useBearContext(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( + 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