当不处于远程调试模式时,ES6代理在React Native中未定义?

时间:2016-08-10 10:57:03

标签: react-native

我正在使用代理包装导入的SDK类,这样我最终可以捕获RequestExceptions,即没有网络连接时显示错误弹出窗口。

应用程序在远程调试模式下正常运行,但是,当我禁用它时,会抛出错误Can't find Variable: Proxy。我是否需要以某种方式明确导入此内容?或者是否有一个替代方法来包装一个类,以便我可以捕获它的所有异常?

以下是代理包装器的代码。

import Backend from 'backend-sdk';

import RequestException from 'backend-sdk/src/exceptions/RequestException';

let handler = {
  get: (target, name, receiver) => {
    try {
      return Reflect.get(target, name, receiver);
    } catch (e) {
      if (e instanceof RequestException) {
        console.error(e);
        //TODO Add a toast notification for failed API requests
      } else {
        throw e;
      }
    }
  }
};

export default new Proxy(new Backend(), handler);

2 个答案:

答案 0 :(得分:6)

默认情况下,代理不会在native native中进行pollyfilled。它适用于chrome调试器,因为在调试过程中本机使用chrome js引擎,请参阅Document on js environment。您可以尝试使用Proxy pollyfill

答案 1 :(得分:1)

请注意,as of react-native 0.59(在Android上为react-native)现在使用了更现代的JavaScriptCore版本,其中包括代理支持。