Skip to content

Instantly share code, notes, and snippets.

@bendman
Created February 2, 2017 18:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bendman/e33243ce5b7104453976335a4739d01a to your computer and use it in GitHub Desktop.
Save bendman/e33243ce5b7104453976335a4739d01a to your computer and use it in GitHub Desktop.
Abstract creating an action that has a type and a payload
// 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