Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

combineActions should accept actionMap #303

Open
examosa opened this issue Jul 6, 2018 · 0 comments
Open

combineActions should accept actionMap #303

examosa opened this issue Jul 6, 2018 · 0 comments

Comments

@examosa
Copy link

examosa commented Jul 6, 2018

Let's say I use createActions(actionMap) to create my actions for a given component.
Example:

actionCreators = createActions({
  USER: {
    VALIDATE: {
      SUBMIT: () => ({ status: "loading" }),
      SUCCESS: response => ({ response }),
      FAIL: error => ({ error })
    },
    SAVE_FIELDS: fields => ({ ...fields })
  }
});

All of these actions will use the same reducer:

const defaultReducer = (state, action) => ({
  ...state,
  ...action.payload
});

I would like to be able to use the following syntax so as to not reproduce the entire map in my reducer:

export default handleActions(
    {
      [combineActions(actionCreators)]: defaultReducer
    },
    initialState
  );

Obviously this doesn't work, as actionCreators is not a list of actions. The workaround I've implemented was to create my own traverseActionMap(actionMap) function which returns an array of all the action creator endpoints of a given actionMap. This in turn allows me to use the following syntax:

export default handleActions(
  {
    [combineActions(...traverseActionMap(actionCreators))]: defaultReducer
  },
  initialState
);

I think it would be great if the combineActions function were to be extended to accept an actionMap parameter and automatically combine the actions for you given the instance you want to map several actions to the same reducer function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant