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.

Error boundary catches errors of child components

See original GitHub issue

Expected behavior

An error thrown by a child of the intersection observer should end up in the user deifned error boundary that was placed outside of the intersection observer.

Current behavior

The intersection observer catches all errors, istead of only handling a missing DOM node situation.

Steps to reproduce

Render an app like this:

import React from 'react';
import IntersectionObserver from '@researchgate/react-intersection-observer';


class MyErrorBoundary extends React.Component {
  state = {hasError: false};

  componentDidCatch(error, info) {
    console.log('I am not called!');
  }

  static getDerivedStateFromError(error) {
    return {hasError: true};
  }

  render() {
    console.log('I am called?');

    return this.state.hasError ? 'oy' : this.props.children;
  }
}

class ChildWithError extends React.Component {
  render() {
    return (
      <button
        onClick={() => {
          this.setState(() => {
            throw new Error('Some forbidden Button was clicked! ');
          });
        }}
      >
        click here to trigger error
      </button>
    );
  }
}

class App extends React.Component {
  render() {
    return (
      <MyErrorBoundary>
        <IntersectionObserver onChange={() => {}} threshold={0.5}>
          <div>
            <ChildWithError />
          </div>
        </IntersectionObserver>
      </MyErrorBoundary>
    );
  }
}

You will find the console.log('I am not called!'); is not called.

Context (environment)

  • Version: 1.1.0 / 1.1.1
  • Platform: Darwin host 18.7.0 Darwin Kernel Version 18.7.0: Thu Jan 23 06:52:12 PST 2020; root:xnu-4903.278.25~1/RELEASE_X86_64 x86_64

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:11 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
Rendezcommented, May 26, 2020

That is a great way to use this. Thank you!

Read more comments on GitHub >

github_iconTop Results From Across the Web

React error handling with react-error-boundary - LogRocket Blog
Error boundaries were introduced in React 16 as a way to catch and handle JavaScript errors that occur in the UI parts of...
Read more >
Handle errors in React components like a pro
Error boundaries are React components that catch JavaScript errors anywhere in their child component tree, log those errors, and display a ...
Read more >
Error boundaries vs. try...catch - Educative.io
Error boundaries are React components capable of catching JavaScript errors anywhere in their child component tree. They log those errors and display a ......
Read more >
How to Handle Errors in React - AppSignal Blog
React Error boundaries are great for catching errors in declarative code (e.g., inside their child component tree). · For other cases, you need ......
Read more >
Catching Errors in React with Error Boundaries
The idea behind error boundaries is that you can wrap any part of your application in a special component – a so-called 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