facebookarchive / redux-react-hook

React Hook for accessing state and dispatch from a Redux store
MIT License
2.16k stars 103 forks source link

mapState function cannot return a function ? #60

Closed xinkule closed 5 years ago

xinkule commented 5 years ago

When I'm using reselect along with redux-react-hook, there is a situation that needs the mapStateToProps function (mapState function in redux-react-hook) to return a function, but it seems this lib hasn't supported it yet ?

屏幕快照 2019-07-09 11 42 08

ianobermiller commented 5 years ago

This would be a good one to add to the docs. Basically, you need to create the selector with useMemo, so it has the same reference for the lifetime of the component:

function MyComponent() {
  const getVisibleTodos = useMemo(() => makeGetVisibleTodos(), []);
  const {todos} = useMappedState(useCallback(state => getVisibleTodos(state,  props)), [props]));
}
xinkule commented 5 years ago

This would be a good one to add to the docs. Basically, you need to create the selector with useMemo, so it has the same reference for the lifetime of the component:

function MyComponent() {
  const getVisibleTodos = useMemo(() => makeGetVisibleTodos(), []);
  const {todos} = useMappedState(useCallback(state => getVisibleTodos(state,  props)), [props]));
}

Thanks for the tips, I almost forgot the useMemo api. Maybe I could just use useMemo to replace the selector if I don't need to reuse ?

xinkule commented 5 years ago

@ianobermiller And I think getVisibleTodos function should be in the dependency list of useCallback hook in the doc ?

ianobermiller commented 5 years ago

Yup, you are right. Can you send a PR please?

On Tue, Jul 9, 2019 at 7:00 PM Albert notifications@github.com wrote:

@ianobermiller https://github.com/ianobermiller And I think getVisibleTodos function should be in the dependency list of useCallback hook in the doc ?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/facebookincubator/redux-react-hook/issues/60?email_source=notifications&email_token=AAG3HC2O3FCNRUAFKZBVKGTP6U7CDA5CNFSM4H7BIRHKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODZSBVMY#issuecomment-509876915, or mute the thread https://github.com/notifications/unsubscribe-auth/AAG3HC4CQEWCHBMN5LP2UJLP6U7CDANCNFSM4H7BIRHA .