从 sentry-expo 迁移

了解如何从 sentry-expo 迁移到 @sentry/react-native

本指南描述了如何在你的 Expo 应用程序中从 sentry-expo 迁移到 @sentry/react-native

首先,从依赖项中移除 sentry-expo

Copied
npm uninstall sentry-expo

安装 @sentry/react-native 包:

Copied
npx expo install @sentry/react-native

将所有 sentry-expo 的导入替换为 @sentry/react-native

Copied
- import * as Sentry from 'sentry-expo';
+ import * as Sentry from '@sentry/react-native';

sentry-expo 导出的 BrowserReact 替换为 @sentry/react

Copied
- import { Browser, React } from 'sentry-expo';
+ import * as Browser from '@sentry/react';
+ import * as React from '@sentry/react';

sentry-expo 导出的 Native 替换为 @sentry/react-native

Copied
- import { Native } from 'sentry-expo';
+ import * as Sentry from '@sentry/react-native';

enableInExpoDevelopment 选项不再受支持。如果你使用了它,请移除并替换为 __DEV__ 检查,或保持开发过程中 SDK 启用。

Copied
Sentry.init({
-  enableInExpoDevelopment: true,
+  enabled: __DEV__,
});

Expo 特定的标签不再默认添加。如果你使用了它们,你可以手动添加:

Copied
import Constants from "expo-constants";
import * as Device from "expo-device";
import * as Updates from "expo-updates";

import * as Sentry from "@sentry/react-native";

Sentry.setExtras({
  manifest: Updates.manifest,
  deviceYearClass: Device.deviceYearClass,
  linkingUri: Constants.linkingUri,
});

Sentry.setTag("expoReleaseChannel", Updates.manifest.releaseChannel);
Sentry.setTag("appVersion", Updates.manifest.version);
Sentry.setTag("appPublishedTime", Updates.manifest.publishedTime);
Sentry.setTag("expoSdkVersion", Updates.manifest.sdkVersion);
Sentry.setTag("deviceId", Constants.sessionId);
Sentry.setTag("appOwnership", Constants.appOwnership || "N/A");
if (Constants.appOwnership === "expo" && Constants.expoVersion) {
  Sentry.setTag("expoAppVersion", Constants.expoVersion);
}
Sentry.setTag("expoChannel", Updates.channel);

sentry-expo 包会自动为 react-native-web 构建切换到 @sentry/react。这种情况不再适用于 @sentry/react-native,它本身支持 react-native-web

请注意,某些功能在 @sentry/react-native 中使用 react-native-web 时可能不受支持或工作方式不同。请在你的应用程序中验证你使用的功能是否按预期工作。

要继续为 react-native-web 构建使用 @sentry/react,请参阅 @sentry/react 以了解更多关于 Web React 包的详细信息。

接下来,设置 Expo 和 Metro 插件以用于 @sentry/react-native