Rollbar React SDK
Proactively discover, predict, and resolve errors in real-time with Rollbar’s error monitoring platform. Start tracking errors today!
React features to enhance using Rollbar.js in React Applications.
This SDK provides a wrapper around the base Rollbar.js SDK in order to provide an
SDK that matches the intention of how to build React Apps with a declarative API, features for the latest React API like
hooks and ErrorBoundaries, as well as simplify using Rollbar within a React SPA.
Key benefits of using Rollbar React are:
- Telemetry: The telemetry timeline provides a list of “breadcrumbs” events that can help developers understand and fix problems in their client-side javascript. Learn more about telemetry.
- Automatic error grouping: Rollbar aggregates Occurrences caused by the same error into Items that represent application issues. Learn more about reducing log noise.
- Advanced search: Filter items by many different properties. Learn more about search.
- Customizable notifications: Rollbar supports several messaging and incident management tools where your team can get notified about errors and important events by real-time alerts. Learn more about Rollbar notifications.
In Beta
It is currently in a public Beta release right now as we push towards a 1.0 release that will have all of the features
we want to provide full capability for using React SDK in your production apps. We expect a 1.0 release to come in the
next month.
Setup Instructions
Prerequisites
These instructions provide an addendum to the Rollbar.js Setup Instructions.
After following those 2 steps, you will be ready.
Install Rollbar React SDK
To install with npm:
npm install @rollbar/react rollbar
To install with yarn:
yarn add @rollbar/react rollbar
To install by adding to package.json, add the following to your project's
package.json file:
…
"dependencies": {
"@rollbar/react": "^0.8.0",
"rollbar": "^2.24.0",
…
}
…
then run either using npm or yarn (or other package manager):
npm install
# OR
yarn install
Usage and Reference
The React SDK is very new and has not been given the full documentation treatment we expect to get from Rollbar Docs,
but that will be coming shortly and a direct link will be put here for your reference.
In the meantime, the basic usage reference is available below.
Simplest Usage Possible
To get you started quickly, here is an example that will get you started right away by providing the easiest and simplest
usage possible:
import React from 'react';
import { Provider, ErrorBoundary } from '@rollbar/react'; // <-- Provider imports 'rollbar' for us
// same configuration you would create for the Rollbar.js SDK
const rollbarConfig = {
accessToken: 'POST_CLIENT_ITEM_ACCESS_TOKEN',
environment: 'production',
};
export default function App() {
return (
{/* Provider instantiates Rollbar client instance handling any uncaught errors or unhandled promises in the browser */}
<Provider config={rollbarConfig}>
{/* ErrorBoundary catches all React errors in the tree below and logs them to Rollbar */}
<ErrorBoundary>
// all other app providers and components - Rollbar will just work
…
</ErrorBoundary>
</Provider>
);
};
Using with Next.js App Router
The first step to using Rollbar with the Next.js App Router is to configure your Rollbar instance.
import Rollbar from 'rollbar';
const baseConfig = {
captureUncaught: true,
captureUnhandledRejections: true,
environment: process.env.NODE_ENV,
};
export const clientConfig = {
accessToken: process.env.NEXT_PUBLIC_POST_CLIENT_ITEM_TOKEN,
...baseConfig,
};
export const serverInstance = new Rollbar({
accessToken: process.env.POST_SERVER_ITEM_TOKEN,
...baseConfig,
});
We suggest you create a single instance for use server side, to be certain there are not more than one, and a config to use in your client side components. React Server Components limit you to passing simple objects as props from Server Components to Client Components. The config object can be used by the Rollbar Provider to construct your Rollbar instance.
There's a fully functional example that showcases the many ways in which you can integrate Rollbar into your modern web-app leveraging React Server Components and the Next.js App Router.
Components
The following components are available as named imports from @rollbar/react.
Provider Component
The Provider component is used to wrap your React App so an instance of Rollbar will be made available within your React tree.
This is a common pattern in React using a custom React Context that is available to the
Components and hooks from this SDK library.
Configuration Only Usage
The simplest way to use the Provider is to provide a configuration as the config prop which will instantiate an
instance of Rollbar for you and provide that to its child tree:
import React from 'react';
import { Provider } from '@rollbar/react';
// same configuration you would create for the Rollbar.js SDK
const rollbarConfig = {
accessToken: 'POST_CLIENT_ITEM_ACCESS_TOKEN',
environment: 'production',
};
export function App(props) {
return <Provider config={rollbarConfig}>…</Provider>;
}
Instance Usage
Sometimes you may need to instantiate an instance of Rollbar before adding it to your App tree. In that case use the
instance prop to pass it to the Provider like this:
import React from 'react';
import Rollbar from 'rollbar';
import { Provider } from '@rollbar/react';
// same configuration you would create for the Rollbar.js SDK
const rollbarConfig = {
accessToken: 'POST_CLIENT_ITEM_ACCESS_TOKEN',
environment: 'production',
};
const rollbar = new Rollbar(rollbarConfig);
export function App(props) {
return <Provider instance={rollbar}>…</Provider>;
}
This method will also work with the global Rollbar instance when using the Rollbar.init(…) method.
React Native Usage
Rollbar provides a React Native SDK which also wraps the Rollbar.js to provide React Native capabilities based on
that specific environment.
To use the Rollbar React SDK with the React Native SDK, pass the instance that it generates to the Provider's
instance prop, like this:
import React from 'react';
import { Client } from 'rollbar-react-native';
import { Provider } from '@rollbar/react';
const rollbarClient = new Client('POST_CLIENT_ITEM_ACCESS_TOKEN');
export function App(props) {
return <Provider instance={rollbarClient.rollbar}>…</Provider>;
}
ErrorBoundary Component
Rollbar's React SDK provides a new ErrorBoundary component which implements the interface for React's Error Boundaries
introduced in React 16.
The ErrorBoundary is used to wrap any tree or subtree in your React App to catch React Errors and log them to Rollbar
automatically.
The ErrorBoundary relies on the Provider above for the instance of Rollbar, so it will utilize
whatever configuration has been provided.
Simple Usage
You can add an ErrorBoundary component to the top of your tree right after the Provider with no additional props
and it will just work:
import React from 'react';
import { Provider, ErrorBoundary } from '@rollbar/react';
// same configuration you would create for the Rollbar.js SDK
const rollbarConfig = {
accessToken: 'POST_CLIENT_ITEM_ACCESS_TOKEN',
environment: 'production',
};
export function App(props) {
return (
<Provider config={rollbarConfig}>
<ErrorBoundary>…</ErrorBoundary>
</Provider>
);
}
Pass props to control behavior
The ErrorBoundary provides several props that allow customizing the behavior of how it will report errors to
Rollbar.
These props take either a value or a function that will be invoked with the error and info from the Error Boundaries
API's componentDidCatch method (i.e. signature is (error, info)).
import React from 'react';
import { Provider, ErrorBoundary, LEVEL_WARN } from '@rollbar/react';
// same configuration you would create for the Rollbar.js SDK
const rollbarConfig = {
accessToken: 'POST_CLIENT_ITEM_ACCESS_TOKEN',
environment: 'production',
};
export function App(props) {
return (
<Provider config={rollbarConfig}>
<ErrorBoundary
level={LEVEL_WARN}
errorMessage="Error in React render"
extra={(error, info) =>
info.componentStack.includes('Experimental')
? { experiment: true }
: {}
}
>
…
</ErrorBoundary>
</Provider>
);
}
Pass a Fallback UI
You may also include a Fallback UI to render when the error occurs so that the User does not experience a broken/blank
UI caused during the render cycle of React.
It can accept a value that is a React Component
import React from 'react';
import { Provider, ErrorBoundary, LEVEL_WARN } from '@rollbar/react';
// same configuration you would create for the Rollbar.js SDK
const rollbarConfig = {
accessToken: 'POST_CLIENT_ITEM_ACCESS_TOKEN',
environment: 'production',
};
const ErrorDisplay = (
{ error, resetError }, // <-- props passed to fallbackUI component
) => (
<div>
<h1>A following error has occured:</h1>
<p>{error.toString()}</p>
</div>
);
export function App(props) {
return (
<Provider config={rollbarConfig}>
<ErrorBoundary level={LEVEL_WARN} fallbackUI={ErrorDisplay}>
…
</ErrorBoundary>
</Provider>
);
}
RollbarContext Component
Use the RollbarContext component to declaratively set the context value used by Rollbar.js when it's sending any
messages to Rollbar.
This works for your ErrorBoundary from above or any other log or message sent to Rollbar while the RollbarContext
is mounted on the tree.