Created
February 2, 2017 18:22
-
-
Save bendman/e33243ce5b7104453976335a4739d01a to your computer and use it in GitHub Desktop.
Abstract creating an action that has a type and a payload
This file contains hidden or 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
// Abstract creating an action that has a type and a payload: | |
// const addTodo = createAction(ADD_TODO); | |
// is equivalent to | |
// const addTodo = (payload) => ({ type: ADD_TODO, payload }); | |
// | |
// Optionally you can pass in a payloadMap when defining an action | |
// to reshape the payload before storing it on the action. | |
import { identity } from 'lodash'; | |
export default function createAction(type, payloadMap) { | |
const payloadCreator = typeof payloadMap === 'function' ? payloadMap : identity; | |
return (...args) => ({ type, payload: payloadCreator(...args) }); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment