From bc91334154270af2aa2207492b630196c5543478 Mon Sep 17 00:00:00 2001 From: Ilya Vorontsov Date: Tue, 2 Jan 2024 17:09:49 +0300 Subject: [PATCH] =?UTF-8?q?Update=20hooks.md=20=E2=80=94=20reselect=20usag?= =?UTF-8?q?e=20with=20multiple=20instances=20simplified?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/api/hooks.md | 37 +------------------------------------ 1 file changed, 1 insertion(+), 36 deletions(-) diff --git a/docs/api/hooks.md b/docs/api/hooks.md index 6ed03157c..188d964e0 100644 --- a/docs/api/hooks.md +++ b/docs/api/hooks.md @@ -224,42 +224,7 @@ export const App = () => { } ``` -However, when the selector is used in multiple component instances and depends on the component's props, you need to ensure that each component instance gets its own selector instance (see [here](https://github.com/reduxjs/reselect#q-can-i-share-a-selector-across-multiple-component-instances) for a more thorough explanation of why this is necessary): - -```jsx -import React, { useMemo } from 'react' -import { useSelector } from 'react-redux' -import { createSelector } from 'reselect' - -const makeSelectCompletedTodosCount = () => - createSelector( - (state) => state.todos, - (_, completed) => completed, - (todos, completed) => - todos.filter((todo) => todo.completed === completed).length - ) - -export const CompletedTodosCount = ({ completed }) => { - const selectCompletedTodosCount = useMemo(makeSelectCompletedTodosCount, []) - - const matchingCount = useSelector((state) => - selectCompletedTodosCount(state, completed) - ) - - return
{matchingCount}
-} - -export const App = () => { - return ( - <> - Number of done todos: - - Number of unfinished todos: - - - ) -} -``` +However, when the selector is used in multiple component instances and depends on the component's props, you need to ensure that selector's memoization behavior is properly configured (see [here](https://reselect.js.org/faq/#can-i-share-a-selector-across-multiple-component-instances) for details). ### Development mode checks