-
-
Save leonidkuznetsov18/2ab6713ca29dba291ebd7ea653334bc1 to your computer and use it in GitHub Desktop.
Redux connect implementation
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 connect(mapStateToProps, mapDispatchToProps) { | |
return function connector(Component) { | |
class ContainerComponent extends React.Component { | |
componentDidMount() { | |
const { store } = this.context; | |
this.unsubscribe = store.subscribe(() => this.forceUpdate()); | |
} | |
componentWillUnmount() { | |
this.unsubscribe(); | |
} | |
render() { | |
const {store} = this.context; | |
var props = Object.assign(mapStateToProps(store.getState()), mapDispatchToProps(store.dispatch)); | |
return <ContainerComponent {...props} />; | |
} | |
} | |
ContainerComponent.contextTypes = { | |
store: React.PropTypes.object | |
}; | |
return ContainerComponent; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment