【问题标题】:ReactJS: Uncaught ReferenceError: Set is not definedReactJS:未捕获的 ReferenceError:未定义集合
【发布时间】:2018-01-12 04:58:19
【问题描述】:

只有在某些浏览器上我才能得到

Uncaught ReferenceError: Set is not defined.

它是使用 create-react-app 创建的 reactjs 应用。

为什么我仍然收到错误消息?
如何使用 polyfill?
reactjs默认不使用polyfill吗?

webpack.config.prod.js

entry: [require.resolve('./polyfills'), paths.appIndexJs],

polyfills.js

'use strict';

if (typeof Promise === 'undefined') {
  // Rejection tracking prevents a common issue where React gets into an
  // inconsistent state due to an error, but it gets swallowed by a Promise,
  // and the user has no idea what causes React's erratic future behavior.
  require('promise/lib/rejection-tracking').enable();
  window.Promise = require('promise/lib/es6-extensions.js');
}

// fetch() polyfill for making API calls.
require('whatwg-fetch');

// Object.assign() is commonly used with React.
// It will use the native implementation if it's present and isn't buggy.
Object.assign = require('object-assign');

// In tests, polyfill requestAnimationFrame since jsdom doesn't provide it yet.
// We don't polyfill it in the browser--this is user's responsibility.
if (process.env.NODE_ENV === 'test') {
  require('raf').polyfill(global);
}

【问题讨论】:

  • 看来我们需要在需要 polyfill 的 reacttjs 组件上使用import 'babel-polyfill'。但我们为什么要这样做? babel loader 不能处理吗?
  • 我在某些 Chrome/Android 浏览器上看到了这个错误,即使我已经用 https://cdn.polyfill.io/v2/polyfill.min.js?features=...,Set,Map"></script> 设置了 polyfill

标签: javascript reactjs


【解决方案1】:

不确定是否有人找到了正确的解决方案。然而,对于较旧的浏览器,尤其是在 Android Kitkat 4.4 浏览器/webview 上,这个问题非常明显。请使用以下链接获取解决方案

https://reactjs.org/docs/javascript-environment-requirements.html

如果你使用的是 ReactJS 16,那么首先包含 babel-polyfill

  • npm install --save babel-polyfill

打开你的 index.js 并导入 babel-polyfill

  • 导入'babel-polyfill'

创建构建并部署。这应该可以解决问题。

【讨论】:

    【解决方案2】:

    我在处理我的 React 项目时遇到了同样的问题。 所以我只安装 babel-polyfill 包。 它现在工作正常。

    【讨论】:

      【解决方案3】:

      似乎是一个不支持ES6 的问题with older WebView
      我在 Genymotion (Android API 21) 上遇到了这个问题,即使在更新 Chrome 之后它也无法正常工作。
      但它适用于物理设备。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-07-13
        • 2023-01-12
        • 2023-01-23
        • 2016-11-03
        • 2011-01-05
        • 2016-01-02
        • 2013-10-06
        相关资源
        最近更新 更多