-
Notifications
You must be signed in to change notification settings - Fork 7.7k
Updated hooks reference #2524
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
base: main
Are you sure you want to change the base?
Updated hooks reference #2524
Conversation
Added a note about throwing an error in default switch case in `useReducer`, as per Dan's [tweet](https://twitter.com/dan_abramov/status/1093969005675773954) and [this issue](reactjs#2159)
Hi JaffParker! Thank you for your pull request and welcome to our community. We require contributors to sign our Contributor License Agreement, and we don't seem to have you on file.In order for us to review and merge your code, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. If you have received this in error or have any questions, please contact us at [email protected]. Thanks! |
Deploy preview for reactjs ready! Built with commit 88fb19f |
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook open source project. Thanks! |
@@ -286,6 +286,10 @@ function Counter() { | |||
> | |||
>React guarantees that `dispatch` function identity is stable and won't change on re-renders. This is why it's safe to omit from the `useEffect` or `useCallback` dependency list. | |||
|
|||
>Note | |||
> | |||
>Unlike reducers in Redux or similar state managing solution, actions in `useReducer` are specific to a component. Hence, it is recommended that you **throw an error in the default switch case**. It can help you track bugs in early stages (for example, mistakes in action references). |
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.
Switch case is one way to write a reducer, but there are others, like an object literal.
>Unlike reducers in Redux or similar state managing solution, actions in `useReducer` are specific to a component. Hence, it is recommended that you **throw an error in the default switch case**. It can help you track bugs in early stages (for example, mistakes in action references). | |
>Unlike reducers in Redux or similar state managing solution, actions in `useReducer` are specific to a component. Hence, it is recommended that you **treat unknown action types as errors** rather than handling them with a default value. It can help you track bugs in early stages (for example, mistakes in action references). |
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.
Also, might be worth mentioning how Flow and Typescript can help prevent dispatching unknown action types.
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 find React docs don't make many references to Typescript, they stay pretty agnostic (although I do agree that it's very helpful). Do you still think it's a good idea to add it? I could work it in.
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.
There are a few and this area is one where a type system can specifically help identify either an unhandled state or runtime error.
Added a note about throwing an error in default switch case in
useReducer
, as per Dan's tweet and this issue