Skip to content
Advertisement

How do I properly use useSelector + createSelector (from ‘reselect’) in React app?

I am using reselect lib in my React project. Here is a code:

JavaScript

Code for selector:

JavaScript

As I mentioned in the App’s code, first commented line (where I destructure the object) works good: I’m fetching tickets from the server, put them in redux state, get them in App’s component and render them.

But if I create selector by createSelector function from ‘reselect’ lib and put it in useSelector function like I mentioned in the code const tickets = useSelector(sorterSelector), I got mistake: TypeError: Cannot read property 'map' of undefined. Tickets are not returned from the state, but state is empty. That means that fetchData function didn’t work.

What am I doing wrong here? How to use it properly?

UPD: this doesn’t work too const tickets = useSelector((state) => sorterSelector(state))

Advertisement

Answer

All you need is proper return in selector:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement