L
L
lexstile2020-05-21 10:46:55
React
lexstile, 2020-05-21 10:46:55

React-redux: hooks or classic connect approach - pros and cons?

The other day I got acquainted with hooks from react-redux , but I didn’t fully understand what the advantages and disadvantages of each of the approaches were, I would be very grateful for the feedback.

I would like to get an answer if we write an application without class components.
They write that connect is a more productive thing, and useSelector will render more often, is this true?

If you use reselect in conjunction with react-redux hooks (useSelector, useDispatch), can you completely replace the connect approach (mapStateToProps && mapDispatchToProps) and expect similar performance?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mikhail Osher, 2020-05-21
@lexstile

They write that connect is a more productive thing, and useSelector will render more often, is this true?

Who said this and what backed up these facts? Sounds like water to me.
reselect should be used regardless of whether hooks or connect are used, but only in cases where the selector must return some computed data.
If you look at the implementation.
connect carries a load of code: mapStateToProps (which has several variations, depending on whether ownProps are used or not), mapDispatchToProps (which creates new functions), mergeProps (you need to check whether it was passed or not), and options on top of all that. Returns a component that does equalty checks and maybe something else. That is, connect on top of the entire wagon of code gives us a component on top that carries logic.
hooks: useSelector stupidly subscribes to the store and does only equality checks, useDispatch takes out the dispatch function via the Context API and does not create any new functions, we just use the ready-made actionCreator.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question