Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#122 Is it possible to use async/await in plain React? #191

Open
01abhishekjain opened this issue Aug 17, 2022 · 4 comments
Open

#122 Is it possible to use async/await in plain React? #191

01abhishekjain opened this issue Aug 17, 2022 · 4 comments

Comments

@01abhishekjain
Copy link

01abhishekjain commented Aug 17, 2022

The answer given is:

If you want to use async/await in React, you will need Babel and transform-async-to-generator plugin. React Native ships with Babel and a set of transforms.

But, you can use it in React, like so:

function App() {
  const click = async function () {
    await Promise.resolve(123);
  };
  return (
    <div onClick={click}>lorem ipsum</div>
  );
}

What am I missing?

@srini-leanfolks
Copy link

Yes, we can do it.

@Charlygraphy23
Copy link

@01abhishekjain if u install react with create-react-app or vite, it will provide you a template project to play with. And the project already bundled with webpack and babel and you don't have to worry about the configuration of react. In this case you can run react app normally and when you build an app by npm run build, then with the help of babel and other transpilers the code get converted to support all browsers.
Now if you want to create a react project from scratch, you need to mannually create the environment like create a webpack config to provide react run environment or babel.
You can still change the configuration of your react app if you use create-react-app or vite

@Bhanu1776
Copy link

Yes, you can do it!

@ArjixWasTaken
Copy link

"modern" browsers don't need a polyfil for Promises...
by modern I mean browser versions since ~2017 and most likely even before then

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants