Last active
October 8, 2016 02:04
-
-
Save farism/77fc324247c63bf453f14ec6635a13ec to your computer and use it in GitHub Desktop.
withThunks
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 React, { PropTypes } from 'react'; | |
export default (thunks = {}) => (Component) => { | |
return class extends React.Component { | |
static propTypes = { | |
dispatch: PropTypes.func, | |
}; | |
static defaultProps = { | |
dispatch: () => {}, | |
}; | |
constructor(props) { | |
super(props); | |
const { dispatch } = props; | |
this.thunks = Object.keys(thunks).reduce((acc, key) => { | |
return { | |
...acc, | |
[key]: (payload) => dispatch(thunks[key](props, payload)), | |
}; | |
}, {}); | |
} | |
render() { | |
return ( | |
<Component ref="inner" {...this.props} thunks={this.thunks} /> | |
); | |
} | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment