Created
May 1, 2016 20:32
-
-
Save markerikson/c971efe9c29a95ea3075744b7b17c5b7 to your computer and use it in GitHub Desktop.
Redux prepareActions utility function
This file contains 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 {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