Skip to content
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

fix(react): Add children prop type to ErrorBoundary component #4966

Merged
merged 4 commits into from
Apr 26, 2022

Conversation

cameronaziz
Copy link
Contributor

@cameronaziz cameronaziz commented Apr 23, 2022

To support React 18, which removed children as an optional prop for functional components, this adds children as an option prop for the ErrorBoundary component.

Fixes #4904

@@ -66,7 +66,7 @@ const INITIAL_STATE = {
* Sentry React SDK ErrorBoundary caught an error invoking your application code. This
* is expected behavior and NOT indicative of a bug with the Sentry React SDK.
*/
class ErrorBoundary extends React.Component<ErrorBoundaryProps, ErrorBoundaryState> {
class ErrorBoundary extends React.Component<React.PropsWithChildren<ErrorBoundaryProps>, ErrorBoundaryState> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @cameronaziz, thank you so much for submitting this PR. We would like to fix this type error and release a patch.

Would you be willing to get rid of the React.PropsWithChildren helper here and instead add a children?: React.ReactNode prop to ErrorBoundaryProps, just like one of the maintainers of react suggests here? If that's done we're good to go!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lforst Are you thinking more on the direction of what my original commit was: ea99673

The issue was it would not build with the children render as a function.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1a575e6 without the | undefined would be perfect imo.

That should be fine typewise. Seems weird that it's not building. Can you update the code? I'll take a look at the build output afterwards.

Copy link
Contributor Author

@cameronaziz cameronaziz Apr 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can, but that ends up making the children within this block a never.

if (typeof children === 'function') {
return children();
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. Apparently @types/react@18 changed the ReactNode type not to be callable anymore. I think up to v17 it had a constructor type of some sort. For me, when using the types from @type/react@18 neither children?: ReactNode nor React.PropsWithChildren works. I think it's best if we go with the changes from ea99673.

Sorry for flip-flopping around. Thank you very much for helping out 🙏

@lforst lforst changed the title Add children prop to ErrorBoundary prop type alias fix(react): Add children prop type to ErrorBoundary component Apr 26, 2022
@lforst lforst merged commit 6824a7a into getsentry:master Apr 26, 2022
@lforst
Copy link
Member

lforst commented Apr 26, 2022

@cameronaziz I took the liberty of taking this over and merging. We'll include the changes in our next patch. Thank you for your contribution!

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

Successfully merging this pull request may close these issues.

Getting Typescript errors when upgrading to React 18
3 participants