是否可以在 React JS 中使用 async/await?

编程入门 行业动态 更新时间:2024-10-24 21:25:01
本文介绍了是否可以在 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 和正确的转换: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?

本文发布于:2023-08-02 17:58:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1279921.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:JS   React   await   async

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!