Chaining / combining ready-made connect functions?
See original GitHub issueSo, it turns out it is very handy and useful to just provide ready-made connect functions for some common use cases.
For example, our data fetching and caching subsystem provides it’s own version of connect()
that works exactly like the react-redux connect
, except it can look at the output of mapStateToProps
and if the values are instances of some special data-fetching-definition objects the same subsystem provides, it knows how to setup the initial data fetch and subsequent updates by passing through dispatch
from mapDispatchToProps
to mergeProps
and so forth.
The problem is in order to do this we had to look at and re-implement connect’s internal handling of mapStateToProps
, mapDispatchToProps
and mergeProps
. This is brittle - we have to rely on keeping the re-implemented behavior in sync with you, even though this is a simple matter so far.
The problem actually gets worse the more of these ready-made connect functions we want to implement. There is a relatively simple connectForm
I wish to add to easily hook-up forms to our form handling subsystem. It just maps a well known area of the redux state (looking up forms by name) to a form’s field
prop, and does a straight-forward map of form-related action creators to their respective event props. This should be trivial, but isn’t, again, because we have to re-implement all that default functionality and carefully merge in the handful of things we want to do.
And worst of all the system breaks down entirely when trying to combine the two (e.g. a form that also needs to fetch its data) - it simply cannot be done.
The approach I have in mind is to pass in the connect function the custom-connects call internally as a 5th argument, but this feels like it’s slowly getting out of hand.
Surely there can be a more well thought out composition mechanism provided by react-redux itself? I have some thoughts on how that could look but before I go down that road I’m wondering if there is existing work towards this, or if I’m missing a much more straight-forward way of solving these kinds of problems.
Issue Analytics
- State:
- Created 7 years ago
- Comments:19 (11 by maintainers)
Top GitHub Comments
I’ve used successfully
flow
andcompose
from lodash to combine HOCs.FYI, you can use the
compose
utility to combine those together in a more readable form: