Skip to content

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

JaffParker
Copy link

Added a note about throwing an error in default switch case in useReducer, as per Dan's tweet and this issue

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)
@facebook-github-bot
Copy link
Collaborator

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!

@reactjs-bot
Copy link

Deploy preview for reactjs ready!

Built with commit 88fb19f

https://deploy-preview-2524--reactjs.netlify.com

@facebook-github-bot
Copy link
Collaborator

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).
Copy link
Collaborator

@alexkrolick alexkrolick Oct 31, 2019

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.

Suggested change
>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).

Copy link
Collaborator

@alexkrolick alexkrolick Oct 31, 2019

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.

Copy link
Author

@JaffParker JaffParker Oct 31, 2019

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.

Copy link
Collaborator

@alexkrolick alexkrolick Oct 31, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants