Created
October 5, 2016 10:14
-
-
Save mattiamanzati/94af9c3b4f4419174bd15e0287ba5790 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 {connect as reduxConnect} from 'react-redux' | |
export const connect = (mapStateToProps = defaultMapStateToProps, reduxMapDispatch, reduxMergeProps, options) => | |
BaseComponent => { | |
const reduxMapState = (state, props) => | |
mapStateToProps(props.selector(state), props, state) | |
const ReduxComponent = reduxConnect( | |
reduxMapState, | |
reduxMapDispatch, | |
reduxMergeProps, | |
options | |
)(BaseComponent) | |
class WithConnectSelector extends Component{ | |
constructor(props, ctx){ | |
super(props, ctx) | |
this.selector = computeSelector(props, ctx) | |
} | |
componentWillReceiveProps(nextProps, nextState, nextContext){ | |
this.selector = computeSelector(nextProps, nextContext) | |
} | |
getChildContext(){ | |
return { | |
selector: this.selector | |
} | |
} | |
render(){ | |
return <ReduxComponent {...this.props} selector={this.selector} /> | |
} | |
} | |
WithConnectSelector.contextTypes = { | |
selector: PropTypes.func | |
} | |
WithConnectSelector.childContextTypes = { | |
selector: PropTypes.func | |
} | |
return WithConnectSelector | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment