Skip to content

Instantly share code, notes, and snippets.

@farism
Last active October 8, 2016 02:04
Show Gist options
  • Save farism/77fc324247c63bf453f14ec6635a13ec to your computer and use it in GitHub Desktop.
Save farism/77fc324247c63bf453f14ec6635a13ec to your computer and use it in GitHub Desktop.
withThunks
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