reactjs-interview-questions icon indicating copy to clipboard operation
reactjs-interview-questions copied to clipboard

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

Open 01abhishekjain opened this issue 3 years ago • 3 comments

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?

01abhishekjain avatar Aug 17 '22 10:08 01abhishekjain

Yes, we can do it.

srini-leanfolks avatar Sep 13 '22 05:09 srini-leanfolks

@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

Charlygraphy23 avatar Apr 06 '23 05:04 Charlygraphy23

Yes, you can do it!

Bhanu1776 avatar Sep 03 '23 05:09 Bhanu1776