-
Notifications
You must be signed in to change notification settings - Fork 12
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
Add "default" section in createReducer() #152
Comments
Below is a workaround that may be used to implement the desired behavior. More than that, it even tries to prevent the "default" part from executing if the state was changed by the "switch" reducer, but it's verbose and make much more than it should. It may be much easier to just create a pull request with this fix, but I've never done it before and don't know how one usually does that. const inc = createAction('INC');
const subtract = createAction('SUBTRACT', res => (v: number) => res(v));
const switchReducer = createReducer(0, handle => [
handle(inc, state => state + 1),
handle(subtract, (state, { payload }) => state - payload),
]);
const reducer: typeof switchReducer = (state, action) => {
const newState = switchReducer(state, action);
if (newState !== state) {
return newState;
}
// this is the "default" part
return state;
}; |
I propose following API: const reducer = createReducer(defaultState, handle => [
handle(increment, state => state + 1),
handle(decrement, state => state - 1),
handle.default((state, action) => state)
]) PRs welcome. Fork the next branch and read |
The changes must be complete. New tests run. There is a couple of other tests that complain about obsolete snapshots, and I'm not sure if I can just ignore them for a moment. |
Some checks have failed. I'll try to investigate them on my own. |
# [3.3.0](v3.2.1...v3.3.0) (2020-10-01) ### Bug Fixes * handle.others instead of handle.default ([ffe271b](ffe271b)) * more explicit typing to avoid TS4025 ([25b78c3](25b78c3)) * type for plain action creator. ([#144](#144)) ([f228b81](f228b81)), closes [#143](#143) ### Features * default handler in createReducer ([0b3dfcf](0b3dfcf)), closes [#152](#152)
# [3.3.0](v3.2.1...v3.3.0) (2020-10-01) ### Bug Fixes * handle.others instead of handle.default ([ffe271b](ffe271b)) * more explicit typing to avoid TS4025 ([25b78c3](25b78c3)) * type for plain action creator. ([#144](#144)) ([f228b81](f228b81)), closes [#143](#143) ### Features * default handler in createReducer ([0b3dfcf](0b3dfcf)), closes [#152](#152)
# [3.3.0](the-dr-lazy/deox@v3.2.2...v3.3.0) (2020-10-01) ### Bug Fixes * handle.others instead of handle.default ([f9aec53](the-dr-lazy@f9aec53)) * more explicit typing to avoid TS4025 ([e703730](the-dr-lazy@e703730)) ### Features * default handler in createReducer ([611184d](the-dr-lazy@611184d)), closes [the-dr-lazy#152](the-dr-lazy#152)
# [3.3.0](the-dr-lazy/deox@v3.2.1...v3.3.0) (2020-10-01) ### Bug Fixes * handle.others instead of handle.default ([ffe271b](the-dr-lazy@ffe271b)) * more explicit typing to avoid TS4025 ([25b78c3](the-dr-lazy@25b78c3)) * type for plain action creator. ([the-dr-lazy#144](the-dr-lazy#144)) ([f228b81](the-dr-lazy@f228b81)), closes [the-dr-lazy#143](the-dr-lazy#143) ### Features * default handler in createReducer ([0b3dfcf](the-dr-lazy@0b3dfcf)), closes [the-dr-lazy#152](the-dr-lazy#152)
# [3.3.0](v3.2.1...v3.3.0) (2020-10-09) ### Bug Fixes * handle.others instead of handle.default ([ffe271b](ffe271b)) * more explicit typing to avoid TS4025 ([25b78c3](25b78c3)) * type for plain action creator. ([#144](#144)) ([f228b81](f228b81)), closes [#143](#143) ### Features * default handler in createReducer ([0b3dfcf](0b3dfcf)), closes [#152](#152)
# [3.3.0](v3.2.1...v3.3.0) (2020-10-17) ### Bug Fixes * handle.others instead of handle.default ([ffe271b](ffe271b)) * more explicit typing to avoid TS4025 ([25b78c3](25b78c3)) * type for plain action creator. ([#144](#144)) ([f228b81](f228b81)), closes [#143](#143) * use symbol for the `others` handler ([3aa61fe](3aa61fe)) ### Features * default handler in createReducer ([0b3dfcf](0b3dfcf)), closes [#152](#152)
There are use cases for this. Two examples how I use it:
One of the easiest ways is to make the third optional argument that will be used as the default reducer:
Do you plan to add something like this? What do you think of this proposal?
The text was updated successfully, but these errors were encountered: