Skip to content

Updated hooks reference - #2524

Open
JaffParker wants to merge 1 commit into
reactjs:mainfrom
JaffParker:patch-2
Open

Updated hooks reference#2524
JaffParker wants to merge 1 commit into
reactjs:mainfrom
JaffParker:patch-2

Conversation

@JaffParker

Copy link
Copy Markdown

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
Copy Markdown
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 cla@fb.com. Thanks!

@reactjs-bot

Copy link
Copy Markdown

Deploy preview for reactjs ready!

Built with commit 88fb19f

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

@facebook-github-bot

Copy link
Copy Markdown
Collaborator

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

@alexkrolick alexkrolick Oct 31, 2019

Copy link
Copy Markdown
Collaborator

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

@alexkrolick alexkrolick Oct 31, 2019

Copy link
Copy Markdown
Collaborator

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.

@JaffParker JaffParker Oct 31, 2019

Copy link
Copy Markdown
Author

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.

@alexkrolick alexkrolick Oct 31, 2019

Copy link
Copy Markdown
Collaborator

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