This article is about fixing 'ErrorBoundary' cannot be used as a JSX component in bvaughn react-error-boundary
  • 31-Jan-2023
Lightrun Team
Author Lightrun Team
Share
This article is about fixing 'ErrorBoundary' cannot be used as a JSX component in bvaughn react-error-boundary

‘ErrorBoundary’ cannot be used as a JSX component in bvaughn react-error-boundary

Lightrun Team
Lightrun Team
31-Jan-2023

Explanation of the problem

The user is encountering a typescript error when using the default first example of a library named “react-error-boundary.” The error message states that “ErrorBoundary” cannot be used as a JSX component.

The version of “react-error-boundary” being used is 3.1.4 and the user is running Node version 14.19.0 and npm version 6.14.16. The code being used is shown below:

import {ErrorBoundary} from ‘react-error-boundary’

function ErrorFallback({error, resetErrorBoundary}) {
  return (
    <>
      <p>Something went wrong:</p>
      <p>{error.message}</p>
      <button onClick={resetErrorBoundary}>Try again</button>
    </>
  )
}

const ui = (
  <ErrorBoundary FallbackComponent={ErrorFallback} onReset={() => {
    // reset the state of your app so the error doesn’t happen again
  }}>
    <ComponentThatMayError />
  </ErrorBoundary>
)

Troubleshooting with the Lightrun Developer Observability Platform

Getting a sense of what’s actually happening inside a live application is a frustrating experience, one that relies mostly on querying and observing whatever logs were written during development.
Lightrun is a Developer Observability Platform, allowing developers to add telemetry to live applications in real-time, on-demand, and right from the IDE.

  • Instantly add logs to, set metrics in, and take snapshots of live applications
  • Insights delivered straight to your IDE or CLI
  • Works where you do: dev, QA, staging, CI/CD, and production

Start for free today

Problem solution for ‘ErrorBoundary’ cannot be used as a JSX component in bvaughn react-error-boundary

The error message encountered by the user is a typescript error that is related to the “ErrorBoundary” component. When attempting to use the “ErrorBoundary” component as a JSX component, the system is unable to recognize it and returns an error message. This is because the typescript library that is in use has undergone a breaking change, specifically in the “@types/react” library version 18.

There are two potential solutions to resolve the issue:

a. Upgrade or update the “@types/react” library: The first solution involves checking the version of the “@types/react” library that is currently in use by running the command “npm ls @types/react.” If version 18.x is in use, the user can either upgrade to the latest version of React or update the “@types/react” library to version 17.x in the lock file.

"dependencies": {
  "@types/react": "17.x"
}

b. Use npm-force-resolutions: The second solution involves installing the “npm-force-resolutions” package and adding a resolution for “@types/react.” The following script and resolution can be added to the “package.json” file:

"scripts": {
 "preinstall": "npx npm-force-resolutions"
}

"resolutions": {
 "@types/react": "17.0.30"
}

By using the “npm-force-resolutions” package, the system will automatically resolve any version conflicts and ensure that the correct version of the “@types/react” library is in use.

The typescript error encountered by the user is related to the “ErrorBoundary” component and is caused by a breaking change in the “@types/react” library version 18. The issue can be resolved by either upgrading to the latest version of React or updating the “@types/react” library to version 17.x in the lock file, or by using the npm-force-resolutions package. The “npm-force-resolutions” package ensures that the correct version of the “@types/react” library is in use, resolving any version conflicts and preventing any future issues from arising.

Other popular problems with react-error-boundary

Problem: Inconsistent TypeScript Definitions

One of the most common issues faced while using react-error-boundary is the inconsistent TypeScript definitions. This problem is caused by the breaking change from @types/react version 18. To check if your dependencies include @types/react@18.x, you can run the command npm ls @types/react.

Solution:

To resolve this issue, you can either upgrade to the latest version of react or update the @types/react@17.x version in your lock file. Here is an example of how to update your lock file:

// package.json
{
  ...
  "dependencies": {
    ...
    "@types/react": "17.0.8",
    ...
  }
}

Problem: Force Resolution Issues

Another common issue while using react-error-boundary is the force resolution problem.

Solution:

To resolve this issue, you can install npm-force-resolutions and add a resolution for all of @types/react. Here is an example of how to add the resolution to your package.json file:

// package.json
{
  ...
  "scripts": {
    "preinstall": "npx npm-force-resolutions"
  },
  "resolutions": {
    "@types/react": "17.0.8"
  }
}

Problem: Incorrect Fallback Component

One of the most prevalent problems while working with react-error-boundary is incorrect FallbackComponent setup.

Solution:

To resolve this issue, make sure you have correctly passed the FallbackComponent prop to the ErrorBoundary component. Here is an example of how to set up a correct FallbackComponent:

import { ErrorBoundary } from "react-error-boundary";

function ErrorFallback({ error, resetErrorBoundary }) {
  return (
    <div>
      Something went wrong:
      {error.message}
      <button onClick={resetErrorBoundary}>Try again</button>
    </div>
  );
}

const UI = (
  <ErrorBoundary fallback={ErrorFallback} onReset={() => {
    // reset the state of your app so the error doesn't happen again
  }}>
    <ComponentThatMayError />
  </ErrorBoundary>
);

A brief introduction to react-error-boundary

React Error Boundary is a feature in React that helps in handling errors in React components during the rendering process. The Error Boundary is a higher-order component that wraps around a React component and catches any errors that occur within that component tree. This feature was introduced in React 16.0 and has been an essential tool for building robust React applications.

A React Error Boundary is defined as a class component that implements the lifecycle method componentDidCatch(). This method is called when an error is caught within the component tree that the Error Boundary is wrapping around. In this method, the error can be logged or displayed to the user in a user-friendly manner. The Error Boundary also provides a way to reset the state of the application when the error is fixed, so that the error doesn’t happen again. This is done through the use of the onReset property, which takes a callback function that resets the state of the app. Additionally, the Error Boundary can also specify a FallbackComponent that is rendered when an error occurs, providing a way to display a custom error message to the user.

Most popular use cases for react-error-boundary

  1. Error handling and logging: React Error Boundary can be used to handle and log errors that occur within a React component tree. This provides a way to keep the application running smoothly, even in the event of an error. When an error occurs, the error can be logged or displayed to the user in a user-friendly manner through the use of the FallbackComponent and componentDidCatch() lifecycle method.
class ErrorBoundary extends React.Component {
  constructor(props) {
    super(props);
    this.state = { hasError: false };
  }

  static getDerivedStateFromError(error) {
    // Update state so the next render will show the fallback UI.
    return { hasError: true };
  }

  componentDidCatch(error, errorInfo) {
    // Log the error to an error tracking service
    logErrorToService(error, errorInfo);
  }

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

    return this.props.children;
  }
}
  1. Improving User Experience: React Error Boundary provides a way to display a user-friendly error message to the user when an error occurs within a React component tree. This can improve the user experience by giving the user a clear indication of what went wrong, rather than just seeing a blank screen or an error message.
  2. Debugging: React Error Boundary provides a way to debug errors that occur within a React component tree. By logging the error and providing information about the error in the FallbackComponent, developers can quickly identify the source of the error and fix it. Additionally, the componentDidCatch() lifecycle method can be used to log the error to an error tracking service, providing more information about the error and making it easier to debug.
Share

It’s Really not that Complicated.

You can actually understand what’s going on inside your live applications.

Try Lightrun’s Playground

Lets Talk!

Looking for more information about Lightrun and debugging?
We’d love to hear from you!
Drop us a line and we’ll get back to you shortly.

By clicking Submit I agree to Lightrun’s Terms of Use.
Processing will be done in accordance to Lightrun’s Privacy Policy.