Created
December 17, 2016 19:35
-
-
Save ronen-e/4dbec96d81e9f20b449d97d173f2bc8c to your computer and use it in GitHub Desktop.
Redux connect implementation
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
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