我开始使用 React Native 进行编程,并且习惯了使用以下语法:
I started programming in React Native, and I got used to use the syntax:
async myFunction(){ ... return await otherFunction(); }但是我不知道如何在共享包中使其与React JS和React Native兼容.我怎样才能做到这一点,以便它在两个平台上都能工作?
But I don't know how to make it compatible with React JS and React Native in a shared package. How can I accomplish this so that it works in both platforms?
谢谢!
推荐答案React Native 附带 Babel 和一些 Babel 预设,而 Web 上的 React 只是与 React 相关的代码.
React Native ships with Babel and some Babel presets, whereas React on the web is just React related code.
如果你今天想在 web 上使用 async/await,你需要 Babel 和正确的转换:babeljs.io/docs/plugins/transform-async-to-generator/
If you want to use async/await on the web today you'll need Babel and the correct transforms: babeljs.io/docs/plugins/transform-async-to-generator/
或 stage-1 预设,这在当今的 React 应用程序中相当普遍.babeljs.io/docs/plugins/preset-stage-1/
or the stage-1 presets, which is fairly common in React apps today. babeljs.io/docs/plugins/preset-stage-1/
更多推荐
是否可以在 React JS 中使用 async/await?
发布评论