Better Programming

Advice for programmers.

Follow publication

Member-only story

React Gets First Class Support for Async/Await — Yay or Meh?

Jose Granja
Better Programming
Published in
5 min readOct 24, 2022

React Gets First Class Support for Async/Await — use hook
Image by Author

The first cornerstone of React’s first-class support for Async/Await started when the team introduced Suspense on React 16.6. Back then, there was still no concurrent mode and its functionality was limited. The suspended component would render and just be hidden on the DOM. It was known as Legacy Suspense.

The Suspense was to become an important mechanism on which all the other APIs would rely.

When concurrent mode landed, the Suspense API was enhanced:

  • The execution of the React Component is “paused”. The Element is not mounted until the ComponentThatSuspends is resolved.
  • Effects/Lifecycle are fired properly.

As of today, lazy loading components is the only use case supported by Suspense. The React team has been spending a long time figuring out how to blend data loading with Suspense. In this published RFC Draft, we can finally see how they plan to build it.

Suspense for Data Fetching

The approach the React team is taking is quite conventional, but with a twist.

They have chosen to implement this feature in two flavors:

  • React Client: by introducing a use hook
  • React Server Components: supporting the native async/await Es7 syntax.

The use hook

This hook is as controversial as its name. They have chosen this particular name in an attempt to differentiate it from the rest. Why? Because this hook is different: it can run conditionally.

This alone breaks the whole conception of hooks that we have learned over the past years. It is quite remarkable how the React team has baked that idea into our heads. There even was a famous post from Dan Abramov explaining why in detail (link here).

How can the use hook be conditionally invoked? Because simplified, it just throws a Promise that is caught by the nearest Suspense parent.

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Jose Granja
Jose Granja

Written by Jose Granja

Top Writer @Medium | 1M+ Views | Lead FE @Lingoda | I write weekly about web-development topics 📰 Support me at https://dioxmio.medium.com/membership 🙇

Responses (2)

Write a response