Skip to content

Instantly share code, notes, and snippets.

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