-
-
Save esamattis/c02f24d8977da7da1513919f0c24be62 to your computer and use it in GitHub Desktop.
Redux Tree Query HOC with Fallback Values
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import { connect } from 'react-redux'; | |
import _ from 'lodash'; | |
function reduxConnected(reduxTreeQuery, fallbackValue) { | |
return WrappedComponent => { | |
const mapStateToProps = (state) => ({ | |
reduxTreeQuery: _.get(state, reduxTreeQuery, fallbackValue) | |
}); | |
class ReduxConnectedComponent extends React.Component { | |
render() { | |
return ( | |
<WrappedComponent reduxTreeQuery={ this.props.reduxTreeQuery } /> | |
); | |
} | |
} | |
return connect( | |
mapStateToProps, | |
null | |
)(ReduxConnectedComponent); | |
} | |
} | |
export default reduxConnected; | |
/* | |
Usage: | |
import React, {Component} from 'react'; | |
import reduxConnected from './redux-connected'; | |
class Age extends Component { | |
render() { | |
return ( | |
<span>{this.props.reduxTreeQuery}</span> | |
) | |
} | |
} | |
const Age = () => (<span>{this.props.reduxTreeQuery}</span>); | |
export default reduxConnected('user.age', '?')(Age); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment