Created
May 1, 2016 16:05
-
-
Save borisd/63ca58a2011bcfb149e24340ec827d54 to your computer and use it in GitHub Desktop.
A super simple 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
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