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

Pass shorthand arguments to reducers #227

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

monvillalon
Copy link

Right now, in order to access the fsa properties you either have to use the the action argument directly or use destructuring:

handleActions({
  ACTION: (state, action ) => {
    return { ...state, data: action.payload.data, user: action.meta.user }
  },
  DESTRUCTURING: (state, { payload: { data }, meta: { user } }) => {
    return { ...state, filters, data, user }
  },
})

I'm proposing a third backward compatible form, that simplifies destructuring for the most common cases and I think looks pleasing and simpler, and makes it obvious those should
be the only properties of an fsa action

handleActions({
  SHORT_HAND_ARGS: (state, action, payload, meta, error ) => {
    return { ...state, data:payload.data, user: meta.user }
  },
  SHORT_HAND_DESTRUCTURING: (state, action, { data }, { meta }, error) => {
    return { ...state, filters, data, user }
  },
})

@yangmillstheory
Copy link
Contributor

Great idea 👍, always a fan of reducing (no pun intended) boilerplate.

To clarify, should the SHORT_HAND_DESTRUCTURING example be

handleActions({
  SHORT_HAND_ARGS: (state, action, payload, meta, error ) => {
    return { ...state, data:payload.data, user: meta.user }
  },
  SHORT_HAND_DESTRUCTURING: (state, _, { data }, { user }, error) => {
    return { ...state, filters, data, user }
  },
})

Also, I wish there were a way to not have the unused argument. Can we introspect using length and act accordingly?

@yangmillstheory yangmillstheory self-requested a review July 8, 2017 05:13
1. `action`: The redux action

and as a shorthand, for easier desctructuring:
3. `payload`: The fsa payload of the action, *action.payload*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/fsa/FSA, please

@@ -29,13 +29,24 @@ import { handleAction } from 'redux-actions';

If a `reducer` function is passed, it is used to handle both normal actions and failed actions. (A failed action is analogous to a rejected promise.) You can use this form if you know a certain type of action will never fail, like the increment example above.

The reducer function gets recieves the following arguments
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/recieves/receives, and we can remove "gets" entirely.


1. `state`: The current redux state

1. `action`: The redux action
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Numbering here is incorrect, should be 2. Also I would say

the Flux Standard Action

instead of

the redux action

Copy link

@chrisbendel chrisbendel Oct 2, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PS markdown suggests using 1. multiple times

  1. A
  2. B
  3. C
1. A
1. B
1. C

as it will auto increment numbers for you, and makes adding a step before/after later on not so tedious reordering all of the numbers


1. `action`: The redux action

and as a shorthand, for easier desctructuring:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spelling: "destructuring"

@@ -28,6 +28,12 @@ export default function handleAction(type, reducer = identity, defaultState) {
return state;
}

return (action.error === true ? throwReducer : nextReducer)(state, action);
return (action.error === true ? throwReducer : nextReducer)(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we destructure here:

const { payload, meta, error } = action
const hasError = error === true
return (hasError ? throwReducer : nextReducer)(state, action, payload, meta, hasError)

@timche timche force-pushed the master branch 4 times, most recently from 11485ca to 4bd68b1 Compare May 8, 2020 15:11
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

Successfully merging this pull request may close these issues.

3 participants