Provide a way to throw error (maybe asynchronous) that can be handled by componentDidCatch
See original GitHub issueDo you want to request a feature or report a bug? Feature request.
What is the current behavior?
componentDidCatch can only handle synchronous error.
What is the expected behavior? The capability of error boundaries to handle asynchronous error.
The error boundaries example in the docs is very similar with how an asynchronous error is handled in React.
class WithAsyncData extends React.Component {
constructor(props) {
super(props);
this.state = { error: null, value: null };
}
// using componentDidMount instead of componentDidCatch
componentDidMount() {
doAsynchronousThing()
.then(value => this.setState({ value }))
.catch(error => this.setState({ error }))
}
render() {
if (this.state.error) {
return <h1>An error occured.</h1>;
}
return this.props.children;
}
}
Unfortunately, the new componentDidCatch lifecycle only aware of synchronous error. I think, it will be
good to provide a way to throw an error asynchronously, like:
class WithAsyncData extends React.Component {
constructor(props) {
super(props);
this.state = { error: null, value: null };
}
componentDidMount() {
doAsynchronousThing()
.then(value => this.setState({ value }))
.catch(error => this.throwError(error)) // throwing error asynchronously
}
render() {
if (this.state.error) {
return <h1>An error occured.</h1>;
}
return this.props.children;
}
}
With that, a network error can be handled in single place with a NetworkErrorBoundary component for example.
Issue Analytics
- State:
- Created 5 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Catching Asynchronous Errors in React using Error Boundaries
Just use setState . Throw inside the callback. See how errors are caught by your Error Boundaries. Let's extract this snippet to a...
Read more >Handling JavaScript errors in React with error boundaries
Use error boundaries in React components to handle JavaScript errors ... UI after an error has been thrown, while componentDidCatch() should ...
Read more >React error boundaries with useEffect and async function ...
Use componentDidCatch() to log error information. ... In your async function catch your exception and throw it inside a react hook callback.
Read more >Error Boundaries - React
Asynchronous code (e.g. setTimeout or requestAnimationFrame callbacks); Server side rendering; Errors thrown in the error boundary itself (rather than its ...
Read more >How to Handle Errors in React - AppSignal Blog
const fetchData = async () => { try { return await fetch("https://some-url-that-might-fail.com"); } catch (error) { console.error(error); // You ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

I think
might do what you’re looking for
@wise-introvert @LinusU
I have found this solution in another issue on this repository but unfortunately cannot remember the author. I’m sorry for that, all credits belong to the original author.