Last active
July 23, 2018 00:53
-
-
Save hex13/72e29e2b4c8051808f30ddcaefa90730 to your computer and use it in GitHub Desktop.
simple way for making universal action creator for Redux
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
'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