Skip to content

Instantly share code, notes, and snippets.

@leonidkuznetsov18
Forked from ronen-e/connect.js
Created December 4, 2019 10:55
Show Gist options
  • Save leonidkuznetsov18/2ab6713ca29dba291ebd7ea653334bc1 to your computer and use it in GitHub Desktop.
Save leonidkuznetsov18/2ab6713ca29dba291ebd7ea653334bc1 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