Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const keyMirror = require('keymirror');
- const ActionSufix = keyMirror({
- REQUEST: null,
- SUCCESS: null,
- ERROR: null,
- });
- const createAction = actionName => {
- const action = {};
- Object.keys(ActionSufix).forEach(sufix => {
- Object.defineProperty(action, sufix, {
- get() {
- const getter = function (actionPayload = {}) {
- return {
- type: getter.type,
- payload: {
- ...actionPayload,
- },
- };
- };
- getter.type = `${actionName}_${sufix}`;
- getter.payload = {};
- return getter;
- },
- });
- });
- return action;
- };
- // create action
- const loadDataAction = createAction('LOAD_DATA');
- // action type
- console.log(loadDataAction.SUCCESS.type);
- // action object
- console.log(loadDataAction.SUCCESS);
- // action with payload
- console.log(loadDataAction.SUCCESS({
- id: 123,
- }));
- // Console output:
- // LOAD_DATA_SUCCESS
- // { [Function: getter] type: 'LOAD_DATA_SUCCESS', payload: {} }
- // { type: 'LOAD_DATA_SUCCESS', payload: { id: 123 } }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement