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 cla@fb.com. 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! |
|
|
||
| >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.
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.
Also, might be worth mentioning how Flow and Typescript can help prevent dispatching unknown action types.
There was a problem hiding this comment.
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.
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