[英]Uncaught ReferenceError: regeneratorRuntime is not defined in React
npm i --save-dev @babel/plugin-transform-runtime
{
"plugins": ["@babel/plugin-transform-runtime"]
}
More Info: https://babeljs.io/docs/en/babel-plugin-transform-runtime更多信息: https://babeljs.io/docs/en/babel-plugin-transform-runtime
TLDR; TLDR;
Thanks It works when I add an import statement -- import regeneratorRuntime from "regenerator-runtime";
谢谢 当我添加一个导入语句时它起作用了—— import regeneratorRuntime from "regenerator-runtime";
in the component i am using async/await.在我使用异步/等待的组件中。
Ran into this problem (using Babel v7) and even after following the advice and installing relevant packages, I was still unable to get id of this error.遇到了这个问题(使用 Babel v7),即使按照建议安装了相关软件包,我仍然无法获得这个错误的 id。 following stack overflow posts were checked...检查了以下堆栈溢出帖子...
Following actions helped:以下行动有所帮助:
"moduleNameMapper": { “模块名称映射器”:{
".+\.(css|styl|less|sass|scss|png|jpg|ttf|woff|woff2)$": "identity-obj-proxy" } ".+\.(css|styl|less|sass|scss|png|jpg|ttf|woff|woff2)$": "identity-obj-proxy" }
--setupFilesAfterEnv "./src/setupTests.js" --setupFilesAfterEnv "./src/setupTests.js"
so to run a test, it will be:所以要运行测试,它将是:
$ jest /pathToTest/TestFile.test.js --setupFilesAfterEnv "./src/setupTests.js" $ jest /pathToTest/TestFile.test.js --setupFilesAfterEnv "./src/setupTests.js"
Hope it helps someone like it helped me...希望它可以帮助像它帮助我的人...
If it's really necessary for you to use the async function then the solutions above should work.如果您确实需要使用异步 function,那么上述解决方案应该可以工作。 Another way to resolve this is to use regular promises, at least that was in my case.解决此问题的另一种方法是使用常规承诺,至少在我的情况下是这样。
just add只需添加
"browserslist": [
"last 2 Chrome versions"
]
at the end of your projects package.json
file, also see that its plural browsers not browser!在您的项目package.json
文件的末尾,还看到它的复数浏览器不是浏览器!
Your file in the end might look something like this ->你的文件最终可能看起来像这样 ->
},
"dependencies": {
"prop-types": "^15.8.0",
"react": "^17.0.1",
"react-dom": "^17.0.1"
},
"browserslist": [
"last 2 Chrome versions"
]
}
ignore the dependency section in the above code view, its just for reference on how your package.json might look.忽略上述代码视图中的依赖部分,它仅用于参考 package.json 的外观。
2022 2022
If you're working with Babel 7 or later version, you don't need to install an extra plugin (neither @babel/plugin-transform-runtime
or @babel/plugin-transform-regenerator
or other plugins ).如果您使用的是 Babel 7 或更高版本,则不需要安装额外的插件( @babel/plugin-transform-runtime
或@babel/plugin-transform-regenerator
或其他插件)。
Later, you have to include this statement every time you're using async/await
syntax.之后,每次使用async/await
语法时都必须包含此语句。
import regeneratorRuntime from "regenerator-runtime";
Maybe if you have set a linter in your project it will warning you about that statement is declared but its value is never read, but I think is just an error, because if you delete it the code doesn't work.也许如果您在项目中设置了一个 linter,它会警告您该语句已声明但它的值从未被读取,但我认为这只是一个错误,因为如果您删除它,代码将不起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.