Skip to content

Instantly share code, notes, and snippets.

@thevangelist
Last active July 13, 2017 19:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save thevangelist/c64ec292b5cf7a87099de7520ff7b1f4 to your computer and use it in GitHub Desktop.
Save thevangelist/c64ec292b5cf7a87099de7520ff7b1f4 to your computer and use it in GitHub Desktop.
Redux Tree Query HOC with Fallback Values
import { connect } from 'react-redux';
import _ from 'lodash';
function reduxConnected(reduxTreeQuery, fallbackValue, propName) {
return connect(state => ({
[propName]: _.get(state, reduxTreeQuery, fallbackValue)
}));
}
export default reduxConnected;
...
// Compose for great good!
const addFooAndBar = _.compose(
reduxConnected("data.foo", "?", "foo"),
reduxConnected("data.bar", "?", "bar")
);
var FooBar = props => <span>{props.foo} {props.bar}</span>;
FooBar = addFooAndBar(FooBar);
// Manual without compose
FooBar = reduxConnected("data.foo", "?", "foo")(FooBar);
FooBar = reduxConnected("data.bar", "?", "bar")(FooBar);
@thevangelist
Copy link
Author

Thanks for @epeli for reducing the boilerplate and standardizing the HOC.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment