如何在 React Native 中导入 ErrorUtils

Chr*_*ris 7 react-native

  • RN 版本:0.50
  • 在安卓上测试,没有在iOS上测试

我正在尝试ErrorUtils.setGlobalHandler按照这个 github 问题中的描述使用:https : //github.com/facebook/react-native/issues/1194

但是,问题中不清楚的是如何导入ErrorUtils。它不在反应文档中:https : //facebook.github.io/react-native/docs/0.50/getting-started.html

以前,在 RN 0.41 中,我可以使用以下命令导入 ErrorUtils,import ErrorUtils from "ErrorUtils"; 但是,在 0.50 中,当我尝试像这样导入 ErrorUtils 时,会收到一个带有以下消息的红色反应弹出窗口:

com.facebook.react.common.JavascriptException: Failed to execute 'importScripts' on 'WorkerGlobalScope': The script at 'http://localhost:8081/index.bundle?platform=android&dev=true&minify=false' failed to load.
Run Code Online (Sandbox Code Playgroud)

我也试过,import { ErrorUtils } from 'react-native';但它似乎不存在那里。错误是:

Cannot read property 'setGlobalHandler' of undefined
Run Code Online (Sandbox Code Playgroud)

如何在 RN 0.50 中正确导入 ErrorUtils?

Leo*_*Leo 8

ErrorUtils 是一个全局变量,因此不需要导入。你可以验证这一点console.log(global.ErrorUtils)

然而,它无论如何都是作为模块导出的(这里)。那里的评论还有更多信息,为什么这样做。

您可以像这样导入模块:

import ErrorUtils from 'ErrorUtils';