Answer the question
In order to leave comments, you need to log in
What is the correct way to use the createSelector function (reselect library) with the useSelector hook in a React app?
I'm making a React project. App component code:
const App = () => {
//const { tickets, isFetching, error } = useSelector(({ tickets }) => tickets) // <- эта строка хорошо отрабатывает
const { isFetching } = useSelector(({ tickets }) => tickets)
const tickets = useSelector(sorterSelector) // <- возвращает undefined
const dispatch = useDispatch()
const renderedTickets = tickets.map((ticket) => { // <- проблема - TypeError: Cannot read property 'map' of undefined
const src = `http://pics.avs.io/99/36/${ticket.carrier}.png`
return (
<Ticket
key={ticket.price + ticket.segments[0].duration}
ticket={ticket}
price={ticket.price}
src={src}
segment={ticket.segments}
/>
)
}).slice(0,5)
useEffect(() => {
dispatch(fetchData())
}, [])
return (
<div className="container">
<Header />
<div className="content">
<Filter />
<div className="content__item">
<div className="content__item__sorting">
<Sorter/>
</div>
{isFetching
?
<img src={preloader} alt="preloader"/>
:
<div className="content__item__tickets">
{renderedTickets}
</div>
}
</div>
</div>
</div>
)
}
const allTickets = state => state.tickets
const selected = state => state.sorter.selected
export const sorterSelector = createSelector(
[allTickets, selected],
(tickets, selec) => { // <- tickets - пустой массив
if (tickets.isFetching || tickets.error) {
return []
}
if (selec === "Самый дешевый") {
[...tickets.tickets].sort((a, b) => {
return a.price - b.price
})
} else {
[...tickets.tickets].sort((a, b) => {
return (a.segments[0].duration + a.segments[1].duration) - (b.segments[0].duration + b.segments[1].duration)
})
}
}
)
const tickets = useSelector(sorterSelector)
, the selector returns undefined (error ). With this notation and the logic I described in the sorterSelector, I don't even see the fetchData function in the useEffect of the App component run.
How to solve this problem?
Recording useTypeError: Cannot read property 'map' of undefined
const tickets = useSelector(state => sorterSelector(state))
also doesn't help. The fetchData function doesn't run, the selector remains undefined.
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question