question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

bug: React Error Boundaries Don't Work in Ionic

See original GitHub issue

Bug 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:

  1. Implement a basic react Error Boundary
  2. Wrap the Error Boundary component around another component with a runtime error.
  3. 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:closed
  • Created 3 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
tnoetzelcommented, Mar 12, 2021

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!

0reactions
ionitron-bot[bot]commented, Apr 11, 2021

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found