简体   繁体   English

Create-React-App 在 Edge 浏览器中不起作用

[英]Create-React-App not working in Edge Browser

I am currently playing around with React, starting with Create-React-App Boilerplate, which I downloaded following https://reactjs.org/docs/create-a-new-react-app.html .我目前正在玩 React,从 Create-React-App Boilerplate 开始,我在https://reactjs.org/docs/create-a-new-react-app.html下载了它。

In Chrome the code works pretty well, however, in Microsoft Edge the brwoser windows remains blank white without any output.在 Chrome 中,代码运行良好,但是,在 Microsoft Edge 中,浏览器窗口保持空白,没有任何输出。

I have not modified the Create-React-Package in any way, however, this also holds true for apps that I wrote according to tutorials: Working great in Chrome, but Edge simply shows a white screen as if no code was executed at all.我没有以任何方式修改 Create-React-Package,但是,这也适用于我根据教程编写的应用程序:在 Chrome 中运行良好,但 Edge 只是显示一个白屏,好像根本没有执行任何代码。

Do I need some additional package to make React Apps run in the Edge Browser?我需要一些额外的包来让 React Apps 在 Edge 浏览器中运行吗?

That is by design.那是设计使然。 If you look in your package.json the dev build is only configured to use the following browsers:如果您查看 package.json,则开发版本仅配置为使用以下浏览器:

 "development": [ "last 1 chrome version", "last 1 firefox version", "last 1 safari version" ]

read https://create-react-app.dev/docs/supported-browsers-features/#configuring-supported-browsers for more information阅读https://create-react-app.dev/docs/supported-browsers-features/#configuring-supported-browsers了解更多信息

You must be using some latest javascript functions like String.prototype.includes() or something like that which is breaking on Edge.您必须使用一些最新的 javascript 函数,例如String.prototype.includes()或类似在 Edge 上崩溃的函数。 You can add polyfill你可以添加polyfill

npm install react-app-polyfill core-js npm install react-app-polyfill core-js

// in App.js or main file // 在 App.js 或主文件中

import 'react-app-polyfill/stable';

I was dealing with IE and Edge compatibility.我正在处理 IE 和 Edge 兼容性。 In my case, I code in some pages就我而言,我在一些页面中编码

windows.scrollTo(0,0);

In Chrome, FireFox and Safari works fine, but has a conflict in Edge and IE.在 Chrome 中,FireFox 和 Safari 工作正常,但在 Edge 和 IE 中存在冲突。
Specifically the scrollTo(0,0) syntax.特别是scrollTo(0,0)语法。 So I tried to change the syntax to所以我尝试将语法更改为

windows.scrollTop = 0;

This simple change solve my problem and I find the solution debugging at IE inspector console, because Edge console does not show me any information.这个简单的更改解决了我的问题,我在 IE 检查器控制台找到了解决方案调试,因为 Edge 控制台不显示任何信息。 Good luck!祝你好运!

If you look in the Developer Tools, you'll see any errors that are happening.如果您查看开发人员工具,您将看到正在发生的任何错误。 If you supply them it will be easier to track down the issue.如果您提供它们,将更容易追踪问题。

For older browsers, it is often a polyfill that you need to add:对于较旧的浏览器,通常需要添加一个 polyfill:

npm install react-app-polyfill

https://www.npmjs.com/package/react-app-polyfill https://www.npmjs.com/package/react-app-polyfill

I have a similar issues with Edge 48. The suggestion for using react-app-polyfill solved some of the issues - in my case it was Array.prototype.flatMap support.我在 Edge 48 上也有类似的问题。使用react-app-polyfill的建议解决了一些问题 - 在我的情况下是Array.prototype.flatMap支持。

However, the polyfills don't fix some language issues.然而,polyfill 并没有解决一些语言问题。 I had used the spread syntax throughout the code, and had to manually replace it.我在整个代码中使用了扩展语法,并且不得不手动替换它。

If you have anything in your code like this:如果您的代码中有这样的内容:

const { x, y, ...rest} = props;
const newProps = {...rest, a:1 };

then you need to replace the spread syntax with something like:那么你需要用类似的东西替换传播语法:

const { x, y } = props;
const rest = {};
Object.keys(props).forEach(key => {
  if(key!=='x' && key !=='y') rest[key] = props[key];
});
const newProps = Object.assign({}, rest, {a:1});

There may be a way to transpile to ES5, but I'm not sure how to do this without seriously messing with the create-react-app default configs.可能有一种方法可以转换为 ES5,但我不确定如何在不严重干扰create-react-app默认配置的情况下执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM