Skip to content

Honeybadger for React Native version <=5

From the root directory of your React Native project:

Terminal window
npm install "@honeybadger-io/react-native"
cd ios && pod install

The above will download the Honeybadger React Native library and add it as a dependency of your project. The iOS step is required to properly add the library to the Xcode project through CocoaPods. Android doesn’t require a separate step.

Add the following to your App.js file to initialize the Honeybadger library.

import Honeybadger from "@honeybadger-io/react-native";
export default function App() {
Honeybadger.configure("PROJECT_API_KEY");
// ...
}

You can log into your Honeybadger account to obtain your API key.

The configure method takes additional configuration options.

NameTypeRequiredDefaultExample
apiKeyStringYES"""hb-api-key-1234"
reportErrorsBooleanNOtrue
revisionStringNO"""8afb34a"
projectRootStringNO"""/path/to/project"
Honeybadger.configure("hb-api-key-1234", "8afb34a", "/path/to/project");

The reportErrors parameter determines if errors are to be sent to Honeybadger. This is set to true by default. In certain environments, say, during development, it could be useful to set reportErrors to false to prevent errors from being posted to your Honeybadger account.

iOS, Android, and JavaScript errors will be automatically handled by the Honeybadger React Native library, by default. But you can also use the following API to customize error handling in your application.

You can use the notify method to send any kind of error, exception, object, String, etc. If sending an error or exception, the Honeybadger React Native library will attempt to extract a stack trace and any relevant information that might be useful. You can also optionally provide additionalData to the notify method, as either a string or an object, to include any relevant information.

If you have data that you would like to include whenever an error or an exception occurs, you can provide that data using the setContext method. Provide an object as an argument. You can call setContext as many times as needed. New context data will be merged with any previously-set context data.

Honeybadger.setContext({
user_id: "123abc",
more: "some additional data",
});

If you’ve used Honeybadger.setContext() to store context data, you can use Honeybadger.resetContext() to clear that data.

Sets the logging level for the Honeybadger library.

Honeybadger.setLogLevel("debug");

The following values are accepted:

ValueMeaning
”debug”Everything will be logged to console.
”warning”Only warnings will be logged to console.
”error”Only errors will be logged to console.

The default logging level is “warning”.