是否可以在 React JS 中使用 async/await? [英] Is it possible to use async / await in React JS?

查看:87
本文介绍了是否可以在 React JS 中使用 async/await?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始使用 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 和正确的转换:https://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: https://babeljs.io/docs/plugins/transform-async-to-generator/

或 stage-1 预设,这在当今的 React 应用程序中相当普遍.http://babeljs.io/docs/plugins/preset-stage-1/

or the stage-1 presets, which is fairly common in React apps today. http://babeljs.io/docs/plugins/preset-stage-1/

这篇关于是否可以在 React JS 中使用 async/await?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆