Skip to content

Instantly share code, notes, and snippets.

@hex13
Last active July 23, 2018 00:53
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 hex13/72e29e2b4c8051808f30ddcaefa90730 to your computer and use it in GitHub Desktop.
Save hex13/72e29e2b4c8051808f30ddcaefa90730 to your computer and use it in GitHub Desktop.
simple way for making universal action creator for Redux
'use strict';
const AC = type => {
const ac = payload => ({type, payload});
ac.type = ac().type;
return { [type]: ac, type }
};
const { addTodo, type: ADD_TODO } = AC('addTodo');
const { removeTodo, type: REMOVE_TODO } = AC('removeTodo');
console.log(ADD_TODO);
console.log(addTodo.type);
console.log(addTodo({id: 10, text: 'do something'}));
console.log(REMOVE_TODO);
console.log(removeTodo.type);
console.log(removeTodo({id: 10}));
// assumptions:
// - action creator is a pure function which does one thing:
// returns an action of given type and with given payload
// (all other things like side effects can be put in e.g. thunks)
// - all actions have same shape and payload property https://github.com/redux-utilities/flux-standard-action
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment