sentry-react-setup by getsentry/sentry-agent-skills
npx skills add https://github.com/getsentry/sentry-agent-skills --skill sentry-react-setup在 React 项目中安装和配置 Sentry。
@sentry/react 或 React 错误边界重要提示: 下面的配置选项和代码示例仅供参考。在实施前,请务必查阅 docs.sentry.io 进行验证,因为 API 和默认值可能已发生变化。
npm install @sentry/react --save
在你的项目根目录下创建 instrument.js(必须在你的应用中首先导入):
import * as Sentry from "@sentry/react";
Sentry.init({
dsn: "YOUR_SENTRY_DSN",
sendDefaultPii: true,
// 追踪 + 会话回放
integrations: [
Sentry.browserTracingIntegration(),
Sentry.replayIntegration(),
],
tracesSampleRate: 1.0,
tracePropagationTargets: [/^\//, /^https:\/\/yourserver\.io\/api/],
replaysSessionSampleRate: 0.1,
replaysOnErrorSampleRate: 1.0,
// 日志
enableLogs: true,
});
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
// src/index.js 或 src/main.jsx
import "./instrument"; // 必须放在最前面!
import App from "./App";
import { createRoot } from "react-dom/client";
const root = createRoot(document.getElementById("app"));
root.render(<App />);
使用错误钩子与 createRoot:
import { createRoot } from "react-dom/client";
import * as Sentry from "@sentry/react";
const root = createRoot(document.getElementById("app"), {
onUncaughtError: Sentry.reactErrorHandler(),
onCaughtError: Sentry.reactErrorHandler(),
onRecoverableError: Sentry.reactErrorHandler(),
});
使用 ErrorBoundary 组件:
import * as Sentry from "@sentry/react";
<Sentry.ErrorBoundary fallback={<p>发生了一个错误</p>}>
<MyComponent />
</Sentry.ErrorBoundary>
| 路由 | 集成 |
|---|---|
| React Router v7 | Sentry.reactRouterV7BrowserTracingIntegration |
| React Router v6 | Sentry.reactRouterV6BrowserTracingIntegration |
| React Router v4/v5 | Sentry.reactRouterV5BrowserTracingIntegration(两者共用) |
| TanStack Router | 参见 TanStack Router 文档 |
import * as Sentry from "@sentry/react";
import { configureStore } from "@reduxjs/toolkit";
const store = configureStore({
reducer,
enhancers: (getDefaultEnhancers) =>
getDefaultEnhancers().concat(Sentry.createReduxEnhancer()),
});
上传源码映射以获得可读的堆栈跟踪:
npx @sentry/wizard@latest -i sourcemaps
SENTRY_DSN=https://xxx@o123.ingest.sentry.io/456
SENTRY_AUTH_TOKEN=sntrys_xxx # 由 sentry-cli / 源码映射使用,SDK 不使用
SENTRY_ORG=my-org # 由 sentry-cli 使用,SDK 不使用
SENTRY_PROJECT=my-project # 由 sentry-cli 使用,SDK 不使用
注意: SDK 会自动读取 SENTRY_DSN。如果使用 Create React App,请加上 REACT_APP_ 前缀;对于 Vite,使用 VITE_。SENTRY_AUTH_TOKEN/ORG/PROJECT 由 sentry-cli 用于源码映射上传,浏览器 SDK 不使用。
添加测试按钮来触发错误:
<button onClick={() => { throw new Error("Sentry 测试错误"); }}>
测试 Sentry
</button>
| 问题 | 解决方案 |
|---|---|
| 未捕获到错误 | 确保首先导入了 instrument.js |
| 源码映射不工作 | 运行源码映射向导,验证认证令牌 |
| React Router 跨度缺失 | 为你的版本添加正确的路由集成 |
每周安装量
1.0K
仓库
GitHub 星标
19
首次出现
2026年1月20日
安全审计
安装于
cursor954
opencode244
codex243
gemini-cli232
github-copilot226
claude-code212
Install and configure Sentry in React projects.
Important: The configuration options and code samples below are examples. Always verify against docs.sentry.io before implementing, as APIs and defaults may have changed.
npm install @sentry/react --save
Create instrument.js in your project root (must be imported first in your app):
import * as Sentry from "@sentry/react";
Sentry.init({
dsn: "YOUR_SENTRY_DSN",
sendDefaultPii: true,
// Tracing + Session Replay
integrations: [
Sentry.browserTracingIntegration(),
Sentry.replayIntegration(),
],
tracesSampleRate: 1.0,
tracePropagationTargets: [/^\//, /^https:\/\/yourserver\.io\/api/],
replaysSessionSampleRate: 0.1,
replaysOnErrorSampleRate: 1.0,
// Logs
enableLogs: true,
});
// src/index.js or src/main.jsx
import "./instrument"; // Must be first!
import App from "./App";
import { createRoot } from "react-dom/client";
const root = createRoot(document.getElementById("app"));
root.render(<App />);
Use error hooks with createRoot:
import { createRoot } from "react-dom/client";
import * as Sentry from "@sentry/react";
const root = createRoot(document.getElementById("app"), {
onUncaughtError: Sentry.reactErrorHandler(),
onCaughtError: Sentry.reactErrorHandler(),
onRecoverableError: Sentry.reactErrorHandler(),
});
Use ErrorBoundary component:
import * as Sentry from "@sentry/react";
<Sentry.ErrorBoundary fallback={<p>An error occurred</p>}>
<MyComponent />
</Sentry.ErrorBoundary>
| Router | Integration |
|---|---|
| React Router v7 | Sentry.reactRouterV7BrowserTracingIntegration |
| React Router v6 | Sentry.reactRouterV6BrowserTracingIntegration |
| React Router v4/v5 | Sentry.reactRouterV5BrowserTracingIntegration (shared for both) |
| TanStack Router | See TanStack Router docs |
import * as Sentry from "@sentry/react";
import { configureStore } from "@reduxjs/toolkit";
const store = configureStore({
reducer,
enhancers: (getDefaultEnhancers) =>
getDefaultEnhancers().concat(Sentry.createReduxEnhancer()),
});
Upload source maps for readable stack traces:
npx @sentry/wizard@latest -i sourcemaps
SENTRY_DSN=https://xxx@o123.ingest.sentry.io/456
SENTRY_AUTH_TOKEN=sntrys_xxx # Used by sentry-cli / source maps, not the SDK
SENTRY_ORG=my-org # Used by sentry-cli, not the SDK
SENTRY_PROJECT=my-project # Used by sentry-cli, not the SDK
Note: The SDK reads SENTRY_DSN automatically. If using Create React App, prefix with REACT_APP_; for Vite, use VITE_. SENTRY_AUTH_TOKEN/ORG/PROJECT are used by sentry-cli for source map uploads, not by the browser SDK.
Add test button to trigger error:
<button onClick={() => { throw new Error("Sentry Test Error"); }}>
Test Sentry
</button>
| Issue | Solution |
|---|---|
| Errors not captured | Ensure instrument.js is imported first |
| Source maps not working | Run sourcemaps wizard, verify auth token |
| React Router spans missing | Add correct router integration for your version |
Weekly Installs
1.0K
Repository
GitHub Stars
19
First Seen
Jan 20, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
cursor954
opencode244
codex243
gemini-cli232
github-copilot226
claude-code212
React 组合模式指南:Vercel 组件架构最佳实践,提升代码可维护性
102,200 周安装