bug: React Error Boundaries Don't Work in Ionic
See original GitHub issueBug Report
Ionic version: 5.5.5
Current behavior: React Error Boundaries don’t seem to work with Ionic. Basic runtime errors in a component aren’t caught by wrapping Error Boundary component.
Expected behavior: Error Boundary should catch error and display fallback UI.
Steps to reproduce:
- Implement a basic react Error Boundary
- Wrap the Error Boundary component around another component with a runtime error.
- Confirm that the Error Boundary doesn’t catch or display an alternate UI.
Related code:
Full example here: https://github.com/tnoetzel/error-boundry-ionic
ErrorBoundary.tsx
import React from 'react';
class ErrorBoundary extends React.Component<any, { hasError: boolean }> {
constructor(props) {
super(props);
this.state = { hasError: null };
}
static getDerivedStateFromError(error) {
// Update state so the next render will show the fallback UI.
return { hasError: true };
}
componentDidCatch(error, errorInfo) {
console.log("ERROR!");
// You can also log the error to an error reporting service
// logErrorToMyService(error, errorInfo);
}
render() {
if (this.state.hasError) {
// You can render any custom fallback UI
return <h1>Something went wrong.</h1>;
}
return this.props.children;
}
}
export default ErrorBoundary;
ExampleComponent.tsx
...
function throwError(shouldError) {
if (shouldError) {
throw Error("I crashed");
} else {
return <></>;
}
}
...
<ErrorBoundary>
{throwError(true)}
</ErrorBoundary>
Other information: N/A
Ionic info:
Ionic:
Ionic CLI : 6.12.3 (/usr/local/lib/node_modules/@ionic/cli)
Ionic Framework : @ionic/react 5.5.5
Capacitor:
Capacitor CLI : 2.4.4
@capacitor/core : 2.4.4
Utility:
cordova-res (update available: 0.15.3) : 0.15.2
native-run (update available: 1.3.0) : 1.2.2
System:
NodeJS : v15.0.1 (/usr/local/Cellar/node/15.0.1/bin/node)
npm : 7.5.2
OS : macOS Big Sur
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (2 by maintainers)
Top Results From Across the Web
React Error Boundaries not working with React - Stack Overflow
For checking your ErrorBoundary , throw an error from a reachable section in the component tree which is not: Event handlers; Asynchronous code...
Read more >Debugging Guide for Apps in iOS Safari and Android Chrome
Ionic Framework's ultimate guide to debugging apps in iOS Safari and Android Chrome. Read our debugging guide to get started with your Ionic...
Read more >How To Use Error Boundaries in React - DigitalOcean
When the app encounters an error, the component completely unmounts itself and the user is left with a blank HTML page. This can...
Read more >React 18, React Redux 8, and TypeScript: What you need to ...
With React 18 currently in the works, React Redux 8, the React ... for users to write type-safe code and produce bug-free applications....
Read more >React Tips and Tricks — Fragments and Error Handling
Error boundaries are components that are displayed when there're errors. They have special hooks like componentDidCatch to let us retrieve error ...
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 FreeTop 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
Top GitHub Comments
I think you’re right @liamdebeasi - Seems like the issue is that error boundaries won’t catch errors unless they’re in a child component, not a child dom element. Apologies!
Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Ionic, please create a new issue and ensure the template is fully filled out.