React Native 对来自条目的对象的Babel支持

osh3o9ms  于 2022-11-17  发布在  React
关注(0)|答案(1)|浏览(152)

我正在通过WebView在React Native内部运行React Web应用程序。
该网站使用Object.fromEntries,这似乎不适用于我正在使用的设备上的浏览器,并导致我的webapp崩溃(没有错误),当我尝试调用Object.fromEntries
该设备运行的是Android 8.1.0,所以我认为它将使用一个不支持Object.fromEntries的旧Android浏览器。
在我的网络应用程序babel配置中,我试图针对Android 8.0,但当调用Object.fromEntries时,应用程序仍然崩溃。

{
  "presets": [
    [
      "@babel/preset-env",
      {
        "loose": true,
        "modules": false,
        "shippedProposals": true,
        "targets": {
          "Android": "8.0",
          "browsers": ["last 2 version"]
        }
      }
    ],
    [
      "@babel/preset-react",
      {
        "useBuiltIns": true,
        "pragma": "React.createElement"
      }
    ],
    "@babel/preset-typescript"
  ],
  "plugins": [
    [
      "@babel/plugin-proposal-class-properties",
      {
        "loose": true
      }
    ],
    "@babel/plugin-syntax-dynamic-import",
    "babel-plugin-macros",
    [
      "@babel/plugin-transform-runtime",
      {
        "helpers": true,
        "regenerator": true
      }
    ]
  ]
}

我需要在Babel配置中包含其他东西吗?或者可能是覆盖它的东西(即Typescript)?

polkgigr

polkgigr1#

我在Android 8 Chrome浏览器中加载网页时也遇到了类似的问题。以下两个更改帮助我解决了这个问题:

1.手动添加多边形填充

添加依赖项

yarn add react-app-polyfill
yarn add object.fromentries

然后,在应用的根目录(index.js)添加以下两个导入:

import 'react-app-polyfill/stable';
import fromEntries from 'object.fromentries';
fromEntries.shim(); // will be a no-op if not needed

2.更改生成目标

我使用的是create-react-app,并没有直接配置babel,因此这对您来说可能会有所不同,但是我将browserslist设置更改为

">0.2%",
    "not dead",
    "not op_mini all"

(This可能根本不需要)

相关问题