CodeMonkey
CodeMonkey

Reputation: 12424

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?

Upvotes: 9

Views: 10484

Answers (1)

Drew Reese
Drew Reese

Reputation: 202761

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

Upvotes: 8

Related Questions