DEV Community

Diego (Relatable Code)
Diego (Relatable Code)

Posted on • Originally published at on

How to quickly add error tracking with Sentry — Relatable Code

Sentry is an error tracking and performance monitoring library that can be quickly hooked up to any existing project. This is especially useful to detect a considerable amount of bugs and can be easily integrated into a React app.

Let’s get started!


First, let’s install the necessary third party libraries

yarn add @sentry/react @sentry/tracing
Enter fullscreen mode Exit fullscreen mode

Sentry recommends initializing Sentry as early as possible. In a React app, this will commonly be the index.jsx/index.tsx

import * as Sentry from '@sentry/react'; 
import { Integrations } from '@sentry/tracing';

   integrations: [new Integrations.BrowserTracing()], 
   tracesSampleRate: 0.2, 
Enter fullscreen mode Exit fullscreen mode

Looking at the code we initialize the application. The property of tracesSampleRate controls the volume of transactions directly to Sentry. This can be any value between 0 - 1. For the time being, I'll leave it as 0.2.

Now in order to get a functional DSN, we have to create a Sentry account. From their page, this is a fairly straightforward process. They even offer a brief section that details how to get it set up in React. We’ll get an option to create a new project whilst selecting a platform:

Sentry platforms
Sentry platform selection

Let’s select React. On the following screen, you’ll see a code snippet much like the one above with the DSN and how to initialize Sentry.


Now that we have Sentry initialized we can go ahead and test it out. My preferred method of integration is wrapping a Sentry Error Boundary around my components with a fallback component:

import * as Sentry from '@sentry/react'; 

<Sentry.ErrorBoundary fallback={<p>This is a fallback</p>}> 
    <MyComponent /> 
Enter fullscreen mode Exit fullscreen mode

In order to force an error let’s create the following code in a component

const errorMethod = () => { throw new Error('Error'); }; 

return ( 
<button onClick={errorMethod} type="button"> 
    Break the world 
</button> );
Enter fullscreen mode Exit fullscreen mode

Upon pressing the button this should generate an error and be sent directly to Sentry.

As an import aside, if you have an ad blocker (uBlockOrigin/Brave browser) make sure to disable it as Sentry uses cookies in order to keep track of the errors and performance monitoring.

After pressing the button you should see something along these lines on the Sentry dashboard:

Sentry issues
Sentry issue entries

And that’s it! Let me know how you feel about Sentry or error tracking in general in the comments below.

Find more content at Relatable Code

Originally published at on October 24, 2021.

Top comments (0)