Skip to content

Instantly share code, notes, and snippets.

@aneurysmjs
Created September 30, 2019 11:33
Show Gist options
  • Save aneurysmjs/07870f9a433a55e470b96bc597d248df to your computer and use it in GitHub Desktop.
Save aneurysmjs/07870f9a433a55e470b96bc597d248df to your computer and use it in GitHub Desktop.
Simplified version of react-redux's connect HOC
const connect = (mapStateToProps) => (Component) => {
return class Connect extends React.Component {
state = mapStateToProps(store.getState());
componentDidMount() {
// когда store меняется, обновит компонент
this.unsubscribe = store.subscribe(this.update);
}
componentWillUnmount() {
this.unsubscribe();
}
update = () => {
this.setState({
...mapStateToProps(store.getState())
});
};
render() {
// это когда компонент получает состояние у store как props
return <Component {...this.state} />;
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment