question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Chaining / combining ready-made connect functions?

See original GitHub issue

So, 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:closed
  • Created 7 years ago
  • Comments:19 (11 by maintainers)

github_iconTop GitHub Comments

4reactions
esamattiscommented, Oct 13, 2016

I’ve used successfully flow and compose from lodash to combine HOCs.

import {flow} from "lodash/fp";

let ConnectedSomeFormComponent = flow(
  connectForm(formName),
  connectDataFetch(dataFetchMapper),
  connect(regularMapStateToProps, regularMapDispatchToProps)
)(SomeFormComponent);

3reactions
markeriksoncommented, Oct 13, 2016

FYI, you can use the compose utility to combine those together in a more readable form:

const combinedHOC = compose(
    connectForm(formName),
    connectDataFetch(dataMapper),
    connect(mapState, mapDispatch)
);

export default combinedHOC(MyComponent);
Read more comments on GitHub >

github_iconTop Results From Across the Web

JavaScript Function Chaining - Medium
Function chaining is a pattern in JavaScript where multiple functions are called on the same object consecutively. Using the same object reference, multiple ......
Read more >
Promises chaining - The Modern JavaScript Tutorial
In this chapter we cover promise chaining. It looks like this: new Promise(function(resolve, reject) { setTimeout(() => resolve(1), 1000); ...
Read more >
DESIGNING DIDACTICAL TOOLS AND MICROWORLDS FOR ...
Abstract. This paper reports on the extensive design activities of the Freudenthal Institute in the last few years on the area of small...
Read more >
Inference Engines - an overview | ScienceDirect Topics
A forward chaining strategy first checks all available factual knowledge for a given problem. It then compares this with the IF parts of...
Read more >
BU-302: Series and Parallel Battery Configurations
Chain links represent cells in series to increase voltage, doubling a link denotes parallel connection to boost current loading. A weak cell may...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found