From 6162c019e50dfcd6503f34339fd9a683beaec0ae Mon Sep 17 00:00:00 2001 From: Ian Obermiller Date: Tue, 25 Dec 2018 06:33:02 -0800 Subject: [PATCH] Readme: don't recommend declaring mapState outside of the function --- README.md | 30 +++--------------------------- 1 file changed, 3 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index ccda2ed..90c2efa 100644 --- a/README.md +++ b/README.md @@ -133,33 +133,7 @@ Runs the given `mapState` function against your store state, just like const state = useMappedState(mapState); ``` -If your `mapState` function doesn't use props or other component state, -declare it outside of your stateless functional component: - -```tsx -import {useMappedState} from 'redux-react-hook'; - -// Note how mapState is declared outside of the function -- this is critical, as -// useMappedState will infinitely recurse if you pass in a new mapState -// function every time. -const mapState = state => ({ - lastUpdated: state.lastUpdated, - todoCount: state.todos.length, -}); - -export default function TodoSummary() { - const {lastUpdated, todoCount} = useMappedState(mapState); - return ( -
-
Count: {todoCount}
-
Last updated: {new Date(lastUpdated).toString()}
-
- ); -} -``` - -If you need to use props or other component state in your `mapState` function, -memoize the function with `useCallback`: +You can use props or other component state in your `mapState` function. It must be memoized with `useCallback`, because `useMappedState` will infinitely recurse if you pass in a new mapState function every time. ```tsx import {useMappedState} from 'redux-react-hook'; @@ -174,6 +148,8 @@ function TodoItem({index}) { } ``` +If you don't have any inputs (the second argument to `useCallback`) pass an empty array `[]` so React uses the same function instance each render. You could also declare `mapState` outside of the function, but the React team does not recommend it, since the whole point of hooks is to allow you to keep everything in the component. + NOTE: You should only call `useMappedState` once per component, in the same way that you'd use `connect` from `react-redux` only once per component. Otherwise the component will have multiple store subscriptions and could rerender multiple times from a single store update. ### `useDispatch()`