Validates usage of Error Boundaries instead of try/catch for errors in child components.
Rule Details
Section titled “Rule Details”Try/catch blocks can’t catch errors that happen during React’s rendering process. Errors thrown in rendering methods or hooks bubble up through the component tree. Only Error Boundaries can catch these errors.
Invalid
Section titled “Invalid”Examples of incorrect code for this rule:
// ❌ Try/catch won't catch render errorsfunction Parent() { try { return <ChildComponent />; // If this throws, catch won't help } catch (error) { return <div>Error occurred</div>; }}Examples of correct code for this rule:
// ✅ Using error boundaryfunction Parent() { return ( <ErrorBoundary> <ChildComponent /> </ErrorBoundary> );}Troubleshooting
Section titled “Troubleshooting”Why is the linter telling me not to wrap use in try/catch?
Section titled “Why is the linter telling me not to wrap use in try/catch?”The use hook doesn’t throw errors in the traditional sense, it suspends component execution. When use encounters a pending promise, it suspends the component and lets React show a fallback. Only Suspense and Error Boundaries can handle these cases. The linter warns against try/catch around use to prevent confusion as the catch block would never run.
// ❌ Try/catch around `use` hookfunction Component({promise}) { try { const data = use(promise); // Won't catch - `use` suspends, not throws return <div>{data}</div>; } catch (error) { return <div>Failed to load</div>; // Unreachable }}
// ✅ Error boundary catches `use` errorsfunction App() { return ( <ErrorBoundary fallback={<div>Failed to load</div>}> <Suspense fallback={<div>Loading...</div>}> <DataComponent promise={fetchData()} /> </Suspense> </ErrorBoundary> );}