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

Component Lifecycle Error Handling #9865

Closed
matthew-rister opened this issue Jun 6, 2017 · 4 comments
Closed

Component Lifecycle Error Handling #9865

matthew-rister opened this issue Jun 6, 2017 · 4 comments

Comments

@matthew-rister
Copy link

Greetings React team,

Let me start off by saying that I am a big fan of the work your team is doing and keep up the good work. With that said, one of the major disadvantages of React is that it sort of throws up when an exception is thrown in one of the lifecycle methods (i.e. null or undefined reference). Although this is ultimately up to the developer to ensure proper error handling, I was wondering if you would consider adding some additional component lifecycle method for catching unhandled exceptions (i.e. componentDidFail). From a developer perspective, this would make my life so much easier and really help to improve the quality of the applications I produce.

Matt

@bvaughn
Copy link
Contributor

bvaughn commented Jun 6, 2017

Hi @matthew-rister,

Version 15 introduced a new experimental error boundary feature (basically a try/catch for the component tree). That feature has been improved significantly for the upcoming version 16 (currently available as an alpha if you're interested in trying it).

If an error occurs in render or a component lifecycle method, React looks up the tree for the nearest ancestor (component) that define a method unstable_handleError. This method is called and given the chance to re-render in a recovery state. If the re-render also fails, React continues looking for the next highest error boundary until it eventually runs out of them. (It functions similar to try/catch behavior.)

The test code for this feature is here if you're interested in learning more about how it works.

I'm going to close this issue for now since it seems to overlap highly with this existing feature. 😄

@bvaughn bvaughn closed this as completed Jun 6, 2017
@matthew-rister
Copy link
Author

Thanks @bvaughn,

It's great to see that there is already work going on in this space and kudos for writing entertaining test cases :)

Matt

@gaearon
Copy link
Collaborator

gaearon commented Jun 7, 2017

I would add that those tests are pretty old and describe the feature as it's supported in 15 (only a few cases work).

The support in React 16 is much more comprehensive and is described in these tests:

https://github.com/facebook/react/blob/master/src/renderers/__tests__/ReactErrorBoundaries-test.js

@bvaughn
Copy link
Contributor

bvaughn commented Jun 7, 2017

Whoops. Thanks Dan. I didn't mean to link to the version of the test in 15-stable.

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

No branches or pull requests

3 participants