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

Adds support for static getDerivedStateFromError(error) #67

Closed
wants to merge 18 commits into from

Conversation

johnhaitas
Copy link
Contributor

This should resolve #65.

It includes code from #66. It is kept as a separate PR deliberately because preact doesn't currently have incoming support for static getDerivedStateFromError(error) preactjs/preact#819

Documentation here:
https://reactjs.org/docs/react-component.html#static-getderivedstatefromerror

@johnhaitas
Copy link
Contributor Author

This current implementation is incorrect per this documentation: https://reactjs.org/docs/react-component.html#error-boundaries

NOTE
Error boundaries only catch errors in the components below them in the tree. An error boundary can’t catch an error within itself.

Updating the code, starting with the tests.

src/index.js Outdated
try {
if (nodeName.getDerivedStateFromProps) c.state = assign(assign({}, c.state), nodeName.getDerivedStateFromProps(c.props, c.state));
else if (c.componentWillMount) c.componentWillMount();
rendered = c.render(c.props, c.state, c.context);
Copy link

Choose a reason for hiding this comment

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

You likely will want to wrap the recursive call to renderString in try/catch instead of the call to the component's render method so only errors of children will be caught.

Copy link
Contributor Author

@johnhaitas johnhaitas Nov 3, 2018

Choose a reason for hiding this comment

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

yea, this was a correct observation, it has been fixed

@johnhaitas
Copy link
Contributor Author

I have updated the tests to verify that the error is in fact being passed to getDerivedStateFromError with 03971f9 or componentDidCatch with c7aba79.

…al components. This produces a much cleaner `try`/`catch` code around the error boundary. Unfortunately it changes the indent of the class based components code.
@developit
Copy link
Member

I commented on #66 but I think it's slightly different here - it seems like React has indicated getDerivedStateFromError() is not going to be supported in string renderer (ever?). Not sure what we should do.

@johnhaitas
Copy link
Contributor Author

i'd say this is not a good idea if react does not support it

@rschristian
Copy link
Member

Closing as implemented in #305, upon the work done here.

Thanks!

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.

Support static getDerivedStateFromError(error)
4 participants