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.

componentDidCatch() doesn't catch native errors

See original GitHub issue

I’m implementing a Markdown renderer, and in some unhandled edge cases, I end up rendering a <View> inside a <Text>, which of course fails with the error: "Views nested within a Text must have a width and height.

Now I’m trying to catch this error using componentDidCatch() to avoid any crashes in production, and to present a nice error message asking the user to fill a bug with the reproduce case. Unfortunately, componentDidCatch doesn’t seem to catch this particular error.

Environment

Environment: OS: macOS Sierra 10.12.6 Node: 9.5.0 Yarn: 1.5.1 npm: 5.6.0 Watchman: 4.7.0 Xcode: Xcode 9.0 Build version 9A235 Android Studio: 3.0 AI-171.4443003

Packages: (wanted => installed) react: 16.2.0 => 16.2.0 react-native: 0.52.0 => 0.52.0

Steps to Reproduce

class ErrorBoundary extends React.Component {
  constructor(props) {
    super(props);
    this.state = { hasError: false };
  }

  componentDidCatch(error, info) {
    // Display fallback UI
    this.setState({ hasError: true });
    // You can also log the error to an error reporting service
    console.log('We did catch', error, info);
  }

  render() {
    if (this.state.hasError) {
      // You can render any custom fallback UI
      return <Text>Something went wrong.</Text>;
    }

    return this.props.children;
  }
}

class CommentListItemComponent extends Component {
  render() {
    return (
      <ErrorBoundary>
        <Text>
          <View>
            <Text>No width and height, will crash</Text>
          </View>
        </Text>
      </ErrorBoundary>
    );
  }
}

Expected Behavior

After dismissing the red screen (which would only appear in dev), the component should display: Something went wrong

Actual Behavior

After dismissing the red screen, nothing is shown. I also tried to use console.log() in my ErrorBoundary’s render(), but nothing gets logged,

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:5
  • Comments:17 (5 by maintainers)

github_iconTop GitHub Comments

9reactions
martsiecommented, Sep 13, 2018

componentDidCatch only catches javascript errors - the error you’re trying to catch is a native error. You can gracefully catch native errors using https://github.com/master-atul/react-native-exception-handler however the app will be crashed at that point and will need to be restarted.

You could possibly wrap react-native-markdown-view in your own component that detects views inside text.

I do think this is a react-native issue that should be solved. react-native modules that supply native bridges should handle likely native crashes that occur due to bad data be passed by either validating the data in js side or catching the exception in the native exception. Text should do the same.

5reactions
machourcommented, Mar 21, 2018

@hramos I think you misread my problem.

I’m fine with not being able to nest a View inside a Text. My problem is componentDidCatch not being fired in that case.

Shouldn’t it be the case?

Read more comments on GitHub >

github_iconTop Results From Across the Web

ComponentDidCatch does not work - Stack Overflow
This doesn't work because componentDidCatch() only works for catching errors thrown by a components children. Here it seems like you are ...
Read more >
Error Boundaries - React
Error boundaries are React components that catch JavaScript errors anywhere in their child component ... Use componentDidCatch() to log error information.
Read more >
Catching Errors in React with Error Boundaries
Error boundaries is the React way to handle errors in your application. It lets you react and recover from runtime errors as well...
Read more >
React error handling with react-error-boundary - LogRocket Blog
A componentDidCatch lifecycle method for performing operations when our error boundaries catch an error, such as logging to an error logging ...
Read more >
componentDidCatch() and Error Boundary — new way of ...
What do we need to handle errors in React 16 gracefully? Just two things: ... A special method — componentDidCatch() — within the...
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