Description
Describe the feature you'd love to see
When an error is thrown from a setState callback, error boundaries don't catch it.
Example code
import { h, render } from "preact";
import { useState, useErrorBoundary, useCallback } from "preact/hooks";
const App = () => {
const [error] = useErrorBoundary();
if (error) {
return <p>Something went wrong</p>;
}
return <BrokenComponent />;
};
const BrokenComponent = () => {
const [,setState] = useState(null);
setTimeout(() => {
setState(() => throw new Error("here"));
}, 1000);
return <span>Hello world</span>;
};
render(<App />, document.body);
Expected behavior
The error boundary in the parent component should catch errors thrown in setState callbacks from children, allowing graceful fallback UI rendering.
Current behavior
The error isn't caught by the error boundary. The component renders initially, then after 1 second when the error is thrown, nothing is displayed and the error is reported in the console.
Motivation
This would be useful for handling asynchronous errors in child components from parent error boundaries, allowing to catch and display errors of async operations performed by child components.
Additional context
I want this because I would like to handle error thrown asynchronously by child components from a parent.
- Similar issue in React: Throwing Error from hook not caught in error boundary facebook/react#14981
- Working demo with React: https://codepen.io/dmail/pen/XJJqeGp?editors=0010