Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use createSelector (or any memoized selector) with hooks without mapStateToProps

I want to create a memoized selector which will update automatically when the state in the redux store changes.

I read about Reselect's createSelector here:

https://redux.js.org/recipes/computing-derived-data

I see that mapStateToProps is being used to connect the selector to the store.. I'm currently using redux with hooks only (useDispatch and useSelector) without using connect(mapStateToProps, mapDispatchToProps).

Is there a way to use createSelector but still without using connect? If not, is there maybe another way to create a memoized selector?

like image 913
Yonatan Nir Avatar asked Dec 24 '19 07:12

Yonatan Nir


1 Answers

Yes, they are nearly equivalent. useSelector takes a pure function that when invoked is passed the entire redux store (i.e. state). Unlike Reselect's selectors however, they do not have the ability to receive passed props (other than through closure or currying). Save for a few edge cases most of your standard Reselect state selectors can be used with react-redux's useSelector hook.

Reselect selectors are memoizing selectors, so here is the section for working with them.

useSelector redux docs

like image 73
Drew Reese Avatar answered Sep 25 '22 13:09

Drew Reese