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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion docs/api/handleAction.md
Original file line number Diff line number Diff line change
Expand Up @@ -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


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"

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

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);
```
Expand Down
19 changes: 19 additions & 0 deletions src/__tests__/handleAction-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,4 +252,23 @@ describe('handleAction()', () => {
.to.deep.equal({ number: 3 });
});
});

describe('with shorthand arguments', () => {
it('recieves shorthand arguments', () => {
const reducer = handleAction(
type,
(state, action, payloadArg, metaArg, errorArg) =>
({ ...state, payloadArg, metaArg, errorArg }),
prevState,
);

const error = new Error();
expect(reducer(prevState, { type, payload: 123 }))
.to.deep.equal({ ...prevState, payloadArg: 123, metaArg: undefined, errorArg: false });
expect(reducer(prevState, { type, payload: 123, meta: 456 }))
.to.deep.equal({ ...prevState, payloadArg: 123, metaArg: 456, errorArg: false });
expect(reducer(prevState, { type, payload: error, meta: 456, error: true }))
.to.deep.equal({ ...prevState, payloadArg: error, metaArg: 456, errorArg: true });
});
});
});
8 changes: 7 additions & 1 deletion src/handleAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)

state,
action,
action.payload,
action.meta,
action.error === true
);
};
}