DEV Community

henry_68930cc9
henry_68930cc9

Posted on

๐Ÿ› ๏ธ Monitor Errors Like a Pro: How to Integrate Sentry in Your React App

If you've ever shipped a React app to production, you know one painful truth: bugs will happen. Sometimes silently. Sometimes destructively. And sometimes your users won't even report them โ€” they'll just leave.

Thatโ€™s where Sentry comes in โ€” your real-time crash and performance monitoring buddy. In this article, Iโ€™ll walk you through how to add Sentry to your React app so you can catch and fix issues before your users rage-quit.

๐Ÿšจ Why Sentry?
Sentry is an open-source error tracking tool that helps developers monitor and fix crashes in real time. It captures:

Unhandled exceptions

Stack traces

Device/browser info

User context

Performance bottlenecks

This makes debugging in production way easier โ€” and faster.

๐Ÿ“ฆ Step 1: Install Sentry
Inside your React project, run:

bash
Copy
Edit
npm install @sentry/react @sentry/tracing
Or with yarn:

bash
Copy
Edit
yarn add @sentry/react @sentry/tracing
โš™๏ธ Step 2: Initialize Sentry
In your index.js (or main.tsx if you're using TypeScript), initialize Sentry:

js
Copy
Edit
import * as Sentry from "@sentry/react";
import { BrowserTracing } from "@sentry/tracing";
import React from "react";
import ReactDOM from "react-dom/client";
import App from "./App";

Sentry.init({
dsn: "https://yourPublicKey@o0.ingest.sentry.io/yourProjectId", // Replace with your actual DSN
integrations: [new BrowserTracing()],
tracesSampleRate: 1.0, // Performance monitoring, between 0.0 and 1.0
});

const root = ReactDOM.createRoot(document.getElementById("root"));
root.render();
๐Ÿง  Youโ€™ll get your DSN after creating a project at sentry.io.

๐Ÿ›ก๏ธ Step 3: Add an Error Boundary
Wrap your app in Sentryโ€™s ErrorBoundary to catch rendering errors automatically:

js
Copy
Edit
import { ErrorBoundary } from "@sentry/react";

const AppWithErrorBoundary = () => (
Something went wrong ๐Ÿ˜ข

}>


);

root.render();
Now any error inside will be caught and reported!

๐Ÿงช Step 4: Trigger a Test Error
Letโ€™s make sure everythingโ€™s working:

js
Copy
Edit
throw new Error("Sentry test error!");
Refresh the app and check your Sentry dashboard โ€” your error should be waiting for you ๐Ÿ‘€.

๐Ÿ“ˆ Bonus: Performance Monitoring
Sentry can track performance too โ€” page load times, slow renders, etc.

Example:

js
Copy
Edit
const transaction = Sentry.startTransaction({ name: "custom action" });
Sentry.getCurrentHub().configureScope(scope => scope.setSpan(transaction));

setTimeout(() => {
transaction.finish();
}, 1000);
This helps you catch performance issues before your users feel the lag.

๐Ÿง  Optional: Add User Context
To help identify who faced the error:

js
Copy
Edit
Sentry.setUser({ id: "12345", email: "john@example.com" });
Add more context to track what happened:

js
Copy
Edit
Sentry.setContext("checkout", {
step: "payment",
method: "credit_card",
});
๐Ÿงน Ignore Known Non-Issues
Filter out noisy or harmless errors:

js
Copy
Edit
Sentry.init({
dsn: "...",
ignoreErrors: ["ResizeObserver loop limit exceeded", "Script error"],
});
๐Ÿš€ Conclusion
Adding Sentry to your React app is one of the smartest early investments you can make. It helps you:

Catch silent errors

Debug faster

Optimize user experience

Gain confidence with every deploy

Start small, iterate, and let Sentry be your co-pilot in production.

Top comments (0)