React

On this page, we get you up and running with Sentry's SDK, so that it will automatically report errors and exceptions in your application.

Don't already have an account and Sentry project established? Head over to sentry.io, then return to this page.

Install

Sentry captures data by using an SDK within your application’s runtime.

Copied
# Using npm
npm install --save @sentry/react @sentry/tracing
# Using yarn
yarn add @sentry/react @sentry/tracing


export const _frontmatter = {}

Configure

Configuration should happen as early as possible in your application's lifecycle.

Copied
import React from "react";
import ReactDOM from "react-dom";
import * as Sentry from "@sentry/react";
import { Integrations } from "@sentry/tracing";
import App from "./App";

Sentry.init({
  dsn: "https://examplePublicKey@o0.ingest.sentry.io/0",
  integrations: [new Integrations.BrowserTracing()],

  // We recommend adjusting this value in production, or using tracesSampler
  // for finer control
  tracesSampleRate: 1.0,
});

ReactDOM.render(<App />, document.getElementById("root"));

// Can also use with React Concurrent Mode
// ReactDOM.createRoot(document.getElementById('root')).render(<App />);

Once this is done, all unhandled exceptions are automatically captured by Sentry.

Add Error Boundary

If you're using React 16 or above, you can use the Error Boundary component to automatically send Javascript errors from inside a component tree to Sentry, and set a fallback UI.

Set Up React Router

The React Router integration is designed to work with our tracing package. Learn more about set up for our React Router Integration.

Apply Redux

To apply Sentry to Redux, learn more about the Redux Integration and its options.

Verify

This snippet includes an intentional error, so you can test that everything is working as soon as you set it up:

Copied
return <button onClick={methodDoesNotExist}>Break the world</button>;

To view and resolve the recorded error, log into sentry.io and open your project. Clicking on the error's title will open a page where you can see detailed information and mark it as resolved.