-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Add support for componentDidCatch Component method #819
Conversation
Hiya! Thanks for all the work here - just reading up on your solution and forming some thoughts. I had a question - was the existing |
test/browser/lifecycle.js
Outdated
it('should be called when child fails in constructor', () => { | ||
class ErrorReceiverComponent extends Component { | ||
componentDidCatch(error) { | ||
this.setState({ "error": error }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
eslint error cause ci failed https://travis-ci.org/developit/preact/builds/266889109?utm_source=github_status&utm_medium=notification
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@paranoidjk Thanks for the tip. Resolved.
@developit |
Not sure - |
Ah, that makes sense. Error handling should pass through non-component ancestors to be compatible with React. Would you prefer this PR keep the adjusted behaviour of |
@rpetrich definitely need to use a second property - using the current one will break higher order components (the existence of that property is used to determine if a component is a HoC). |
@developit This is ready for an updated review. I took the liberty of naming the new property |
@rpetrich perfect, that's exactly the name I would have chosen! I'll re-review this. |
src/vdom/component.js
Outdated
base._component = componentRef; | ||
base._componentConstructor = componentRef.constructor; | ||
} | ||
base._component = component; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removing the conditional & crawl here seems like it would break component composition. When re-rendering a component that is a child of another component, it will update the _component
property in the DOM do point to itself, which is then incorrect.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I removed these changes in #886 and verified the tests still pass. Wasn't sure how/if I could amend this PR directly.
Would it be possible to split up some of the changes here? Right now this adds |
b616cc8
to
d54d337
Compare
…defined; Add tests for _ancestorComponent/_parentComponent
Here is the React error boundary test suite. It took us a long time to iron out the edge cases so it would be great to aim for at least partial parity. https://github.com/facebook/react/blob/master/src/renderers/__tests__/ReactErrorBoundaries-test.js |
@gaearon Would a recursive React error boundary continue to bubble up the tree or bail? |
It stops at the closest boundary. If that boundary throws attempting to re-render, it bubbles up again, and the process repeats until we reach the root. If we have reached the root, and the error is still unhandled, we unmount the whole thing. |
What happens to errors thrown in |
Yes, errors in all lifecycle hooks are caught, including Both However, since Here is an example of the interplay between a broken |
@gaearon Hey Dan! I'm attempting to get this going again. Is there an updated link to that test suite? |
@sampotts Currently it's stable and usable by referencing the branch as an npm dependency, but the PR is somewhat stalled (which is understandable considering that it's a substantial adjustment, increases the API surface area, and has performance implications) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
@lukeed Is there a way to unset your review to unblock this PR (since the "request changes" status was an accident)? Or if you have time, could you take another look and leave more comments or approve? |
I done goofed! Can't review carefully quite yet, but at least I won't block this :)
Latest Preact 8.3.0 release has been merged in. |
@rpetrich just tested your current branch code, seems it throws
at this line on error, but componentDidCatch works p.s.: not sure what this code does, but seems |
@studentIvan Do you have a test case I could examine? |
@rpetrich sorry it's on my project, so only if we can skype/etc and I will share my screen to you. |
@studentIvan |
Until this is merged, is there some workaround? I need to be able to catch exceptions in my root component and respond to them. |
I use preact from studentIvan/preact#componentDidCatch-support before merge and base errors resolution |
I am also having to use studentIvan/preact#componentDidCatch-support. What needs to be done to merge this? |
What about support for |
…cause `preact`'s support for `componentDidCatch()` does not pass this argument (preactjs/preact#819 (comment))
Will this get merged in 2019 do we think? |
@sampotts |
Awesome. Can't wait 👍 |
Awesome! |
Fantastic! 🎉 |
@pierpo An alpha release is scheduled for March 4th✌️ See: https://twitter.com/marvinhagemeist/status/1097973028426788864 |
@marvinhagemeister Amazing! 😍 |
The alpha release is out 🎉 |
Allow components to catch errors that occur during child components' constructors or lifecycle methods. For simplicity's sake does not pass a second "info" argument like React does. If no component catches and handles a thrown exception, DOM will be left in an inconsistent state as it is in react 8.2.1