React integration guide

Add Bugsnag to your React projects to automatically capture and report errors in production.

New to Bugsnag? Create an account



Install Bugsnag and the Bugsnag / React integration from the npm registry using npm or yarn:

npm install --save @bugsnag/js @bugsnag/plugin-react
# or
yarn add @bugsnag/js @bugsnag/plugin-react

Basic configuration

This documentation is for version 7 of the Bugsnag JavaScript notifier. If you are using older versions, we recommend upgrading to the latest release using our Upgrade guide. Documentation for the previous release can be found on our legacy pages.

Depending on which module system you are using, you’ll need to include Bugsnag in one of the following ways:

// ES module-style import
import Bugsnag from '@bugsnag/js'
import BugsnagPluginReact from '@bugsnag/plugin-react'

// commonjs/node-style require
var Bugsnag = require('@bugsnag/js')
var BugsnagPluginReact = require('@bugsnag/plugin-react')

To start Bugsnag with the React integration, instantiate the BugsnagPluginReact and pass it along with your API key to Bugsnag.start as configuration:

  apiKey: 'YOUR_API_KEY',
  plugins: [new BugsnagPluginReact()],
  otherOptions: value

You can find your API key in Project Settings.

For information on values that can be set in the configuration object, see configuration options.

Capturing React render errors

To catch all render errors in your application and show a custom error screen to your users, follow this example:

const ErrorBoundary = Bugsnag.getPlugin('react').createErrorBoundary(React)

export default () =>
  <ErrorBoundary FallbackComponent={ErrorView}>
    <App />

class App extends React.Component {
  // Your main app component

class ErrorView extends React.Component {
  // This component will be displayed when an error boundary catches an error

When render errors happen, they will be reported to Bugsnag along with any React-specific info that was available at the time.

See React’s documentation on Error Boundaries to find out more.

In the dashboard, you’ll see errors reported with extra debugging info in a “React” tab. For example:

React error information in the the dashboard

Customizing the error boundary

The <ErrorBoundary /> component accepts some additional props:

  • onError() – this allows you to pass in an onError callback which runs only for errors caught by the error boundary.
  • FallbackComponent – by default the error boundary will attempt to re-render the child tree which may result in nothing being rendered at all. If you specify a <FallbackComponent/>, when an error happens Bugsnag will render this instead. This means you can display a user-friendly error state.
  • clearError() – resets the state of the error boundary which will cause a re-render the child tree instead of the <FallbackComponent/>.

Showing full stacktraces

Source maps enable Bugsnag to show you the original file, line, method and surrounding code in your stacktraces.

Use the source maps guide to ensure your tooling outputs source maps, and the build integrations guide to find out how to upload them to Bugsnag.

TypeScript support

Type definitions are provided and will be picked up automatically by the TypeScript compiler when you import any of the top-level @bugsnag/* packages.

Reporting unhandled errors

After completing installation and basic configuration, unhandled exceptions and unhandled promise rejections will be reported and automatically appear on your Bugsnag dashboard.

Reporting handled errors

Sometimes it is useful to manually notify Bugsnag of a problem. To do this, call Bugsnag.notify(). For example:

try {
} catch (e) {

When reporting handled errors, it’s often helpful to send custom diagnostic data or to adjust the severity of particular errors. For more information, see reporting handled errors.

Sending diagnostic data

Automatically captured diagnostics

Bugsnag will automatically capture the following data for every exception:

  • The current URL
  • Script content (if the error originated in an inline <script/> tag)
  • Browser name, version, user agent, locale and time
  • Operating system
  • Release stage (production, beta, staging, etc)

Attaching custom diagnostics

It can often be helpful to attach application-specific diagnostic data to error reports. This can be accomplished by setting a callback which will be invoked before any reports are sent to Bugsnag.

The following adds a map of data to the “company” tab on the Bugsnag dashboard for all captured events:

  onError: function (event) {
    event.addMetadata('company', {
      name: "Acme Co.",
      country: "uk"

For more information, see Customizing error reports.

Identifying users

In order to correlate errors with customer reports, or to see a list of users who experienced each error, it is helpful to capture and display user information on your Bugsnag dashboard.

You can set the user information of an error report using the user configuration property when Bugsnag starts or via an onError callback.

  onError: function (event) {
    event.setUser('3', '', 'Bugs Nag')

For information on doing so, see Adding user data.

Logging breadcrumbs

In order to understand what happened in your application before each crash, it can be helpful to leave short log statements that we call breadcrumbs. A configurable number of breadcrumbs are attached to each error report to help diagnose what events led to the error.

Automatically captured breadcrumbs

By default, Bugsnag captures the following events as breadcrumbs.

  • Clicks
  • Errors
  • Console logs, warnings, and errors
  • Page load, hide, and show
  • DOMContentLoaded events
  • Pop state
  • History push state and replace state
  • Hash change
  • HTTP requests

For more information or to disable particular classes of automatic breadcrumb generation see configuration options.

Attaching custom breadcrumbs

You can use the leaveBreadcrumb method to log potentially useful events in your own applications:

Bugsnag.leaveBreadcrumb('Button clicked')

Bugsnag will keep track of the time and order of the breadcrumbs and show them on your dashboard. Additional data can also be attached to breadcrumbs by providing the optional metadata parameter.

For more information and examples for how custom breadcrumbs can be integrated, see Customizing breadcrumbs.

Session tracking

Bugsnag tracks the number of “sessions” that happen within your application. This allows you to compare stability scores between releases and helps you to understand the quality of your releases.

Sessions are captured and reported by default. This behavior can be disabled using the autoTrackSessions configuration option.

In the browser, Bugsnag will automatically report a session each time:

  • The page loads
  • The URL changes via history.pushState() or history.replaceState()

For more information about manually controlling session tracking, see Capturing sessions.

Tracking releases

Configure your app version to see the release that each error was introduced in.

Bugsnag.start({ appVersion: '4.10.0' })

Then set up a build tool integration to enable linking to code in your source control provider from the releases dashboard, timeline annotations, and stack traces.

Next steps

  • View @bugsnag/js, the library powering Bugsnag for JavaScript, on GitHub
  • Get support for your questions and feature requests