ReactJS:未捕获的ReferenceError:未定义Set

Mah*_*esh 8 javascript reactjs

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

未捕获的ReferenceError:未定义Set.

它是使用create-react-app创建的reactjs应用程序.

为什么我仍然得到错误?
我如何使用polyfill?
反应是否默认使用polyfill?

webpack.config.prod.js

entry: [require.resolve('./polyfills'), paths.appIndexJs],
Run Code Online (Sandbox Code Playgroud)

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);
}
Run Code Online (Sandbox Code Playgroud)

小智 8

不确定是否有人找到了正确的解决方案.但是对于较旧的浏览器,特别是在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'

创建构建和部署.这应该可以解决问题.