Skip to content

Instantly share code, notes, and snippets.

@tommymarshall
Created May 3, 2023 14:18
Show Gist options
  • Save tommymarshall/649024df7b922fcf3f72d0a42b1503cb to your computer and use it in GitHub Desktop.
Save tommymarshall/649024df7b922fcf3f72d0a42b1503cb to your computer and use it in GitHub Desktop.
What can't error boundaries catch, with examples (via Chat GPT)

React error boundaries are a useful feature that allows you to catch and handle errors that occur during rendering, rather than letting them crash your entire application. However, there are some scenarios in which error boundaries may not be able to catch errors. Here are some examples:

  1. Errors that occur outside of the component hierarchy: React error boundaries can only catch errors that occur within their component tree. If an error occurs outside of the boundary's tree, it will not be caught. For example:
function App() {
  return (
    <ErrorBoundary>
      <MyComponent />
    </ErrorBoundary>
  );
}

function MyComponent() {
  throw new Error('Something went wrong');
}

ReactDOM.render(<App />, document.getElementById('root'));

In this example, the error thrown in MyComponent will be caught by the ErrorBoundary. However, if the error was thrown outside of the App component (e.g., in the ReactDOM.render call), the ErrorBoundary would not be able to catch it.

  1. Asynchronous errors: React error boundaries can only catch errors that occur during rendering. If an error occurs during an asynchronous operation (e.g., a setTimeout callback), it will not be caught by the boundary. For example:
function App() {
  return (
    <ErrorBoundary>
      <MyComponent />
    </ErrorBoundary>
  );
}

function MyComponent() {
  useEffect(() => {
    setTimeout(() => {
      throw new Error('Something went wrong');
    }, 1000);
  }, []);

  return <div>My Component</div>;
}

ReactDOM.render(<App />, document.getElementById('root'));

In this example, the error thrown in the setTimeout callback will not be caught by the ErrorBoundary.

  1. Errors in event handlers: React error boundaries can only catch errors that occur during rendering. If an error occurs in an event handler (e.g., a onClick function), it will not be caught by the boundary. For example:
function App() {
  return (
    <ErrorBoundary>
      <MyComponent />
    </ErrorBoundary>
  );
}

function MyComponent() {
  function handleClick() {
    throw new Error('Something went wrong');
  }

  return <button onClick={handleClick}>Click me</button>;
}

ReactDOM.render(<App />, document.getElementById('root'));

In this example, the error thrown in the handleClick function will not be caught by the ErrorBoundary.

It's important to note that React error boundaries are not a catch-all solution for handling errors in your application. They can help you handle errors in your component tree, but you may need to use other techniques (such as try/catch blocks) to handle errors in other parts of your application.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment