Skip to content

Instantly share code, notes, and snippets.

@markerikson
Created May 1, 2016 20:32
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markerikson/c971efe9c29a95ea3075744b7b17c5b7 to your computer and use it in GitHub Desktop.
Save markerikson/c971efe9c29a95ea3075744b7b17c5b7 to your computer and use it in GitHub Desktop.
Redux prepareActions utility function
import {bindActionCreators} from "redux";
import {connect} from "react-redux";
import {action1, action2} from "myActions";
// A reusable utility function that binds action creators
// and returns them as a prop named "actions"
function prepareActions(actions) {
return (dispatch) => ({
actions : bindActionCreators(actions, dispatch);
})
}
const mapDispatch = prepareActions({action1, action2});
const SomeComponent = (props) => (
<div>
<button onClick={props.actions.action1}>Do Action 1</button>
<button onClick={props.actions.action2}>Do Action 2</button>
</div>
);
export default connect(null, mapDispatch)(SomeComponent);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment