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

Reducers: add default handler (without init action handling) #264

Open
wants to merge 10 commits into
base: master
Choose a base branch
from

Conversation

jin60641
Copy link

@jin60641 jin60641 commented Dec 19, 2023

Description

Adds a defaultHandler for reducers
apply #241 's comments

Related issues:

Checklist

  • I have read CONTRIBUTING.md
  • I have linked all related issues above
  • I have rebased my branch

For bugfixes:

  • I have added at least one unit test to confirm the bug have been fixed
  • I have checked and updated TOC and API Docs when necessary

For new features:

  • I have added entry in TOC and API Docs
  • I have added a short example in API Docs to demonstrate new usage
  • I have added type unit tests with dts-jest
  • I have added runtime unit tests with dts-jest

@jin60641 jin60641 mentioned this pull request Dec 19, 2023
9 tasks
@jin60641
Copy link
Author

@piotrwitek
I've made changes to not handle the init action by default since it's not a public API. Do you also think this structure is appropriate?
Additionally, I believe it would be beneficial to have a test code to check if the defaultReducer is not being called when the init action is dispatched. What do you think?

@jin60641 jin60641 changed the title Reducers: add default handler Reducers: add default handler (without init action handling) Dec 19, 2023
Copy link
Owner

@piotrwitek piotrwitek left a comment

Choose a reason for hiding this comment

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

Thanks for the PR, looks good although I have few comments. Best!

@@ -92,6 +129,8 @@ export function createReducer<TState, TRootAction extends Action = RootAction>(
);
}
return reducer(state, action);
} else if (defaultReducer && !initializationActionTypes.test(action.type)) {
Copy link
Owner

Choose a reason for hiding this comment

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

!initializationActionTypes.test(action.type) is not needed user code in default handler should handle the @@redux/init action if needed by just returning the unchanged state.

Copy link
Author

@jin60641 jin60641 Jan 7, 2024

Choose a reason for hiding this comment

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

done 9c38e11

But if the defaultHandler's return value is different from initialState, it may not work as intended by some users who are not aware of the existence of redux INIT (due to the redux INIT action triggering the defaultHandler. initialState will change to defaultHandler's return value). This might be confusing for them. What do you think about this issue?

Copy link
Owner

Choose a reason for hiding this comment

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

That is a trade-off. The default use case is to clear the state and that's that we are aming for to solve as main case. If someone want to use it for some other rare edge case, adding that extra condition check is not a big deal. We just need to put that info in the docs.

Copy link
Owner

Choose a reason for hiding this comment

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

@jin60641 could you please add it to the readme?

Copy link
Author

Choose a reason for hiding this comment

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

done 33f9db8

src/create-reducer.ts Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
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.

Add default handler in createReducer
3 participants