Skip to content

Instantly share code, notes, and snippets.

@borisd
Created May 1, 2016 16:05
Show Gist options
  • Save borisd/63ca58a2011bcfb149e24340ec827d54 to your computer and use it in GitHub Desktop.
Save borisd/63ca58a2011bcfb149e24340ec827d54 to your computer and use it in GitHub Desktop.
A super simple redux connect() implementation
import store from 'store/store';
const connect = (mapStateToProps) => (Component) => {
class Connect extends React.Component {
constructor() {
super();
this.state = {};
}
componentDidMount() {
this.unsubscribe = store.subscribe(this.update.bind(this));
}
componentWillUnmount() {
if (this.unsubscribe) this.unsubscribe();
}
update() {
this.setState(mapStateToProps(store.getState()));
}
render() {
return (
<Component { ...this.state } />
)
}
}
return Connect;
};
export default connect(mapStateToProps)(Component);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment