Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mattiamanzati/94af9c3b4f4419174bd15e0287ba5790 to your computer and use it in GitHub Desktop.
Save mattiamanzati/94af9c3b4f4419174bd15e0287ba5790 to your computer and use it in GitHub Desktop.
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