Last active
July 12, 2017 15:29
-
-
Save hkjpotato/08992b7ebe95162931ecf717eb55d0b6 to your computer and use it in GitHub Desktop.
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
function connectHOC(mapStateToProps, mapDispatchToProps) { | |
return function wrapWithConnect(WrappedComponent) { | |
class Connect extends Component { | |
constructor(props, context) { | |
super(props, context) | |
this.store = context.store | |
this.initSelector() | |
// ...subscribtion | |
} | |
initSelector() { | |
//selector: (reduxStore.state + ownProps) => injected mergedProps | |
this.selector = simpleSelector //the one we defined in the medium story | |
} | |
//data source 1: store state change | |
onStateChange() { | |
this.selectorProps = this.selector(store.state, nextProps) | |
this.setState({}) | |
} | |
//data source 2: ownProps change | |
componentWillReceiveProps(nextProps) { | |
this.selectorProps = this.selector(store.state, nextProps) | |
} | |
componentDidUpdate() { | |
//after the current component get updated, go ahead and notify the nested subscription | |
this.subscription.notifyNestedSubs() | |
} | |
render() { | |
return createElement(WrappedComponent, this.selectorProps) //get the mergedProps from the selector | |
} | |
} | |
return Connect; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment