-
Notifications
You must be signed in to change notification settings - Fork 298
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
||
1. `state`: The current redux state | ||
|
||
1. `action`: The redux action | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Numbering here is incorrect, should be
instead of
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PS markdown suggests using 1. multiple times
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 |
||
|
||
and as a shorthand, for easier desctructuring: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Spelling: "destructuring" |
||
3. `payload`: The fsa payload of the action, *action.payload* | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
4. `meta`: The metadata of the action, *action.meta* | ||
5. `error`: Boolean flag determining if this is an error, *action.error* | ||
|
||
If the reducer argument (`reducer`) is `undefined`, then the identity function is used. | ||
|
||
The third parameter `defaultState` is required, and is used when `undefined` is passed to the reducer. | ||
|
||
###### EXAMPLE | ||
```js | ||
handleAction('APP/COUNTER/INCREMENT', (state, action) => ({ | ||
handleAction('APP/COUNTER/INCREMENT', (state, action, payload, meta, error) => ({ | ||
counter: state.counter + action.payload.amount, | ||
}), defaultState); | ||
``` | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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)( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) |
||
state, | ||
action, | ||
action.payload, | ||
action.meta, | ||
action.error === true | ||
); | ||
}; | ||
} |
There was a problem hiding this comment.
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.