Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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