Skip to content

Instantly share code, notes, and snippets.

@farism
Created June 2, 2016 08:34
Show Gist options
  • Save farism/b0433bf7baf44ffd8588803652d4dc3c to your computer and use it in GitHub Desktop.
Save farism/b0433bf7baf44ffd8588803652d4dc3c to your computer and use it in GitHub Desktop.
withActions
import React, { PropTypes } from 'react';
import { camelize } from './';
export default (actionTypes = []) => (Component) => {
return class extends React.Component {
static defaultProps = {
$: PropTypes.func,
dispatch: PropTypes.func,
};
static propTypes = {
$: (action) => action,
dispatch: () => {},
};
constructor(props) {
super(props);
const { $, dispatch } = props;
this.actions = actionTypes.reduce((acc, type) => {
return {
...acc,
[camelize(type)]: (payload) => dispatch($({ type, payload })),
};
}, {});
}
render() {
return (
<Component {...this.props} actions={this.actions} />
);
}
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment