React Native

了解如何设置 Sentry 的 React Native SDK。

继续阅读以了解如何设置 Sentry 的 React Native SDK,该 SDK 将自动报告应用程序中的错误和异常。如果你更喜欢视频指导,请参阅 如何在 60 秒内安装 Sentry React Native SDK

如果你还没有 Sentry 账户和项目,请前往 sentry.io 注册,然后返回此页面。

除了捕获错误,你还可以通过启用跟踪来监控多个服务或应用程序之间的交互。你还可以收集和分析来自真实用户的性能配置文件,使用性能分析

选择你想要安装的 Sentry 功能(除了错误监控),以获取相应的安装和配置说明。

Sentry 通过在应用程序运行时使用 SDK 来捕获数据。这些 SDK 是特定于平台的,允许 Sentry 深入了解你的应用程序的工作原理。

要安装,请运行 @sentry/wizard

Copied
npx @sentry/wizard@latest -i reactNative

Sentry Wizard 将相应地修补你的项目,但如果你更喜欢,也可以手动设置。你只需要修补项目一次。然后你可以将修补后的文件添加到版本控制系统中。

Sentry Wizard 将执行以下任务
  • 安装 @sentry/react-native 包。
  • @sentry/react-native/metro 添加到 metro.config.js Metro 配置中。
  • @sentry/react-native/expo 添加到 app.json Expo 配置中。
  • 启用 Sentry React Native Gradle 构建步骤以自动上传生成的源映射和调试符号。
  • 包装 Xcode 项目构建阶段脚本 Bundle React Native 代码和图像 以上传生成的源映射并收集捆绑的 node 模块。
  • 添加 Xcode 项目构建阶段 Upload Debug Symbols to Sentry
  • 运行 pod install
  • ios/sentry.properties, android/sentry.propertiesenv.local 中存储构建凭据。
  • layout.tsx/App.tsx 文件中配置 Sentry 以使用提供的 DSN。

如果你使用 Expo,请参阅我们的文档以了解如何将 Sentry 添加到你的 Expo 项目中。此 SDK 适用于托管和裸项目。

为了捕获所有错误,请尽快初始化 Sentry React Native SDK。

App.js
Copied
import * as Sentry from "@sentry/react-native";

Sentry.init({
  dsn: "https://examplePublicKey@o0.ingest.sentry.io/0",
  // Set tracesSampleRate to 1.0 to capture 100% of transactions for tracing.
  // We recommend adjusting this value in production.
  // Learn more at
  // https://docs.sentry.io/platforms/javascript/configuration/options/#traces-sample-rate
  tracesSampleRate: 1.0,
  // profilesSampleRate is relative to tracesSampleRate.
  // Here, we'll capture profiles for 100% of transactions.
  profilesSampleRate: 1.0,
});

为了自动使用 触摸事件跟踪自动跟踪 仪器化你的应用,请用 Sentry.wrap 包装它:

App.js
Copied
export default Sentry.wrap(App);

如果您的应用没有单一的父 "App" 组件,则不需要这样做。

通过添加以下代码片段来验证你的应用是否向 Sentry 发送事件。该代码片段包含一个故意的错误。你应该在几分钟内在 Sentry 中看到该错误报告。

Copied
throw new Error("My first Sentry error!");