Skip to content

Support Additional arguments for handleAction(s) #161

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

Closed
wants to merge 5 commits into from

Conversation

xiaohanzhang
Copy link
Contributor

@xiaohanzhang xiaohanzhang commented Nov 18, 2016

resolve #148

increment, (state, { payload }, extraArg) => ({
counter: state.counter + payload + extraArg
}),
{ counter: 3 });
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 put the closing parenthesis on a newline, and use the same number of arguments in handleActions-test.js?

});

const counterBase = 5;
const counterFix = 1;
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 use extraArg1 and extraArg2 instead, to mirror the parameter names you used above?

@@ -147,4 +147,21 @@ describe('handleActions', () => {
counter: 7
});
});

it('accepts additional arguments', () => {
// meaningless base and fix just for testing additional arguments
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this comment necessary?


export default function handleActions(handlers, defaultState) {
const reducers = ownKeys(handlers).map(type => handleAction(type, handlers[type]));
const reducer = reduceReducers(...reducers);
const reducer = (state, action, ...args) => reducers.reduce(
(p, r) => r(p, action, ...args), state);
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't like these one-letter variable names.

Can we please use:

reducers.reduce((previousState, reducer) => reducer(previousState, action, ...args), state)

Also, at this point, we should remove reducer-reducers from package.json, since we're no longer using it.

And, could you submit a pull request here for passing the extra arguments?

}),
{ counter: 3 });

const counterBase = 10;
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's call this extraArg; counterBase doesn't really signify anything.

* remove reduceReducers from dependency
@yangmillstheory
Copy link
Contributor

Can we update the README to indicate that extra arguments are passed?

Thanks for the contribution. I'd like to wait over the weekend to see if @timche has any thoughts, and then merge.

@yangmillstheory
Copy link
Contributor

Sorry, we just merged v1.0.0. Can you resolve conflicts?

Conflicts:
	README.md
	package.json
	src/handleAction.js
	src/handleActions.js
@yangmillstheory
Copy link
Contributor

@timche will take a look tomorrow.

@timche
Copy link
Member

timche commented Nov 19, 2016

Before we add this feature, I'd like to discuss this a bit deeper: #148

@lsunsi
Copy link

lsunsi commented Oct 2, 2017

I think this change is good. I realize it violates reducer signature if the users use the additional arguments, but from what I can gather, this is a great way of passing global state as a read only to a slice reducer that can write to it's own slice.

The pattern seems fine and the change seems harmless, since it would change literally anything, it'll just be less opinionated on how you use yours reducers which are, as we know, just functions.

+1 for the change

@timche
Copy link
Member

timche commented May 7, 2018

Closing because it's already open for very long time. Don't see it being merged. If you still think a necessity, feel free to comment.

@timche timche closed this May 7, 2018
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.

Additional arguments for handleAction{,s} reducers
4 participants