Skip to content
ClapelloNotes by Barath
ReactCh 16 · 4/6
Core3 min readUpdated

React / 91

use(): read a promise or context

TL;DR

React 19's use() reads a promise, suspending until it resolves, or reads context, even inside an if. The one rule is that the promise must outlive the render.

Reading a promise

Recipe.jsx
import { use } from "react";
 
export default function Recipe({ recipePromise }) {
  const recipe = use(recipePromise); // waits here until it resolves
  return <h2>{recipe.name}</h2>;
}

While the promise is pending, the component suspends: React stops rendering it and shows the nearest <Suspense> fallback instead. When the promise resolves, React renders Recipe again, and this time use returns the value.

pending

Suspense

shows the nearest fallback

fulfilled

use() returns the value

the component renders normally

rejected

Error boundary

shows the nearest error fallback
A component that calls use(promise) needs two helpers above it: Suspense for waiting and an error boundary for failures.
App.jsx
<ErrorBoundary message="Couldn't load the recipe.">
  <Suspense fallback={<p>Loading recipe…</p>}>
    <Recipe recipePromise={recipePromise} />
  </Suspense>
</ErrorBoundary>

ErrorBoundary is the class from Error boundaries. You can't use try/catch around use, so the boundary is how you catch a rejected promise.

The one rule: the promise must outlive the render

created during render ✗

use(fetchReviews())
suspends, then re-renders

A brand-new promise again

It's pending again, so React waits again. The fallback can stay up forever and the request repeats.

created once, outside render ✓

const p = fetchReviews() // in a click, loader or cacheuse(p)
suspends, then re-renders

The same promise, now resolved

use(p) returns the value straight away and the content shows.
use() suspends until the promise settles, then React renders again and asks for the same promise. Only a promise that survives re-renders can ever be 'done'.

So where can a stable promise come from?

  • An event handler, stored in state: onClick={() => setPromise(fetchRecipe(id))}. That's what the demo below does.
  • A cache that hands back the same promise for the same input.
  • A framework or library: a router loader, a Server Component passing a promise down, or a Suspense-ready data library such as TanStack Query.
recipe-api.js
const cache = new Map();
 
export function fetchRecipe(id) {
  if (!cache.has(id)) {
    const promise = fetch(`https://dummyjson.com/recipes/${id}`).then((res) => {
      if (!res.ok) throw new Error(`HTTP ${res.status}`);
      return res.json();
    });
    cache.set(id, promise);
  }
  return cache.get(id);
}

Now use(fetchRecipe(id)) is safe to write in render, because the second call returns the first promise. A real app also needs to expire old entries and retry failures, which is why libraries and frameworks usually own this cache.

use(promise) with Suspense

Try it

No promise yet. Nothing has been requested.

Load the reviews: Suspense shows the fallback until the promise resolves. Then re-render the parent: no fallback, because use() gets the same, already-resolved promise.

Reading context, even inside an if

use(SomeContext) reads context just like useContext. Unlike a Hook, it can sit inside a condition or loop:

Price.jsx
import { use } from "react";
import { CurrencyContext } from "./CurrencyContext.js";
 
export default function Price({ amount, showCurrency }) {
  if (!showCurrency) return <span>{amount}</span>;
  const currency = use(CurrencyContext); // fine after an early return
  return <span>{currency} {amount}</span>;
}

With useContext, that early return before the Hook call would break the rules of Hooks. With use, it's allowed.

Gotchas

Recall

Can you answer these?

0/4

Answer out loud (or in your head) before you tap. That's what makes it stick.

Saved on this device only. No login.