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)