Skip to content

React integration guide

Typical installation time: 5 minutes

Hi there! You’ve found Honeybadger’s guide to React error and exception tracking. Once installed, Honeybadger will automatically report errors from your React application.

Add @honeybadger-io/react as a dependency.

# npm
npm add @honeybadger-io/js @honeybadger-io/react --save
# yarn
yarn add @honeybadger-io/js @honeybadger-io/react

In your main.js:

import React from "react";
import ReactDOM from "react-dom";
import "./index.css";
import App from "./App";
import { Honeybadger, HoneybadgerErrorBoundary } from "@honeybadger-io/react";
const config = {
apiKey: "PROJECT_API_KEY",
environment: "production",
revision: "git SHA/project version",
};
const honeybadger = Honeybadger.configure(config);
ReactDOM.render(
<HoneybadgerErrorBoundary honeybadger={honeybadger}>
<App />
</HoneybadgerErrorBoundary>,
document.getElementById("root"),
);
honeybadger
The Honeybadger config object.
children

Your root <App /> component.

ErrorComponent (optional — default: “DefaultErrorComponent”)

The component that will be rendered in ErrorBoundary children’s place when an error is thrown during React rendering. The default value for this prop is the DefaultErrorComponent.

class DefaultErrorComponent extends Component {
render() {
return (
<div className="error">
<div>An Error Occurred</div>
<div>{this.error}</div>
<div>{this.info}</div>
</div>
);
}
}

Using the example configuration above, you’ll install @honeybadger-io/react as React’s error handler.

Additionally, by default, an error handler for all JavaScript errors will be attached to the window.onerror handler for JavaScript errors that may originate from React components or other JavaScript on the page.

Because React doesn’t intercept all errors that may occur within a React component, errors that bubble up to the window.onerror handler may be missing some React component contextual information, but the stack trace will be available.

If, for some reason, you do not wish to install Honeybadger’s error handler on the global window.onerror handler, you may add { enableUncaught: false } to the configuration you’re passing to Honeybadger.configure.

You may also manually report errors by directly invoking the honeybadger.js API.

honeybadger.notify(error);

See the full documentation for more options.

Honeybadger can track which users have encountered each error. To identify the current user in error reports, add a user identifier and/or email address with honeybadger.setContext:

honeybadger.setContext({
user_id: 123,
user_email: "user@example.com",
});

Sometimes additional application state may be helpful for diagnosing errors. You can arbitrarily specify additional key/value pairs when you invoke setContext.

honeybadger.setContext({
active_organization: 55,
custom_configuration: false,
});

If your user logs out or if your context changes during the React component lifetime, you can set new values as appropriate by invoking setContext again.

Additionally, if needed, you can clear the context by invoking clear:

// Set the context to {}
honeybadger.clear();

@honeybadger-io/react is built on honeybadger.js.

See the Honeybadger JavaScript integration documentation for additional customization options.

As with vanilla JavaScript applications, you can notify Honeybadger when you’ve deployed a new build. Honeybadger will associate an error report with a specific revision number (matching the ‘revision’ field in the configuration passed to Honeybadger.configure).

Here’s a simple curl script to record a deployment:

Terminal window
HONEYBADGER_ENV="production" \
HONEYBADGER_REVISION="$(git rev-parse HEAD)" \
HONEYBADGER_REPOSITORY="$(git config --get remote.origin.url)" \
HONEYBADGER_API_KEY="Your project API key" \
&& curl -g "https://api.honeybadger.io/v1/deploys?deploy[environment]=$HONEYBADGER_ENV&deploy[local_username]=$USER&deploy[revision]=$HONEYBADGER_REVISION&deploy[repository]=$HONEYBADGER_REPOSITORY&api_key=$HONEYBADGER_API_KEY"

Be sure that the same revision is also configured in the @honeybadger-io/react library. Read more about deploy tracking in the API docs.

If you are deploying your site to Netlify, you can notify Honeybadger of deployments via Netlify’s webhooks. Go to the Deploy notifications section of the Build & deploy tab for your site settings, and choose to add an Outgoing webhook notification. Choose Deploy succeeded as the event to listen for, and use this format for your URL:

https://api.honeybadger.io/v1/deploys/netlify?api_key=YOUR_HONEYBADGER_API_KEY_HERE

The environment that will be reported to Honeybadger defaults to the Netlify environment that was deployed, but you can override that with &environment=CUSTOM_ENV in the webhook URL, if you like.

Honeybadger can automatically un-minify your code if you provide a source map along with your minified JavaScript files. See our Source Map Guide for details.

When an error occurs, a form can be shown to gather feedback from your users. Honeybadger can automatically show the form by setting the showUserFeedbackFormOnError prop to true:

<HoneybadgerErrorBoundary
honeybadger={honeybadger}
showUserFeedbackFormOnError={true}
>
<App />
</HoneybadgerErrorBoundary>

Read more about this feature here.

A minimal implementation is included in the example folder in the @honeybadger-io/react repository.

To run it from the command line, enter the following commands in your shell:

Terminal window
cd example
yarn install
REACT_APP_HONEYBADGER_API_KEY=yourkey yarn start

Observe the command-line output to determine the appropriate URL to connect to in your browser (usually http://localhost:3000/).