-
-
Notifications
You must be signed in to change notification settings - Fork 15.3k
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 check that reducer returns current state for unknown actions #1054
Conversation
And fix test case
Ah, good call. I think this looks good to me. I'll let Dan chime in if he wants to. |
Hey, thanks for the PR! Let's hold this off for now. I need to understand this change better. I think DevTools currently actually rely on |
FWIW, DevTools has a different INIT signature (and in the next branch), so this shouldn't cause problems with it. |
Remove extra line break
@msafi is there any particular reason for using |
@nettofarah |
default: | ||
return undefined |
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.
Can we keep the test that if you forget a default
case you'll get this warning? It's an important thing to remember to check.
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.
Okay, I added it here.
* Add a test case to verify reducer has a catch-all clause for unknown action types * Improve error message wording
`not be undefined.` | ||
`Reducers should never return undefined. Make sure this reducer ` + | ||
`has a catch-all clause for unknown action types and that it returns a ` + | ||
`default initial state if the state passed to it is undefined.` | ||
) |
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.
I changed the error message here because if the user doesn't have a default
case in their code, this is the error that will be thrown.
@msafi Could you update this against master? We can get the ball rolling again and get this merged in, hopefully 😄 |
@timdorr Sorry about the delay. I updated the code against master. Please let me know if there's anything else I need to do! |
Sorry for the long wait. Alas, I just remembered why we can't rely on this. We previously rejected a similar change in #174 (comment) because
Potentially this might have problems for static type checkers as well since we're calling a function with a string This is how #174 got superseded by #193 which is roughly what you see now. |
Ah, good catch! So I guess there's no way to have a runtime check that a reducer returns given state for unknown action type. Alright, but there's another issue that this PR tried to fix, which is that there's still a way for the user to handle I'll see if I can send another PR that only addresses this second issue. |
Hey folks,
First ever open source contribution here and a Redux newb, so I have no idea what I'm doing! Alright, with that out of the way...
I was reading the test cases for
combineReducers
and noticed that this test doesn't really test what it intends to test because if you change thedefault
case to return a value, the reducer will not throw and the test will fail.I changed the way Redux inits reducers by making it do an init and a random probe at the same time. This will make it impossible for users to handle private Redux actions, so there's no need for that test anymore. However, I altered the test to make it assert that Redux checks reducers to make sure they return the current state for an unknown action.