Skip to content

Instantly share code, notes, and snippets.

@hkjpotato
Last active July 12, 2017 15:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hkjpotato/08992b7ebe95162931ecf717eb55d0b6 to your computer and use it in GitHub Desktop.
Save hkjpotato/08992b7ebe95162931ecf717eb55d0b6 to your computer and use it in GitHub Desktop.
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