hardFrontend

What does the `use()` hook do, and why is it special?

528 views
01

Understand the problem

Reading promises and context during render with Suspense integration.

reactsuspensedata-fetching
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Stuck? AI Nudge Available

Get a conceptual hint to guide your logic without spoiling the final implementation.

03

Study the solution

The solution is waiting

Give it an honest attempt first — then compare your thinking with the full walkthrough.

04

Read the code

use(promise) with Suspense
import { use, Suspense } from "react";

function Greeting({ namePromise }) {
  const name = use(namePromise);     // suspends until the promise resolves
  return <h3>Hello, {name}!</h3>;
}

// the promise is created OUTSIDE render (stable), then passed in
const namePromise = new Promise((res) => setTimeout(() => res("Ada"), 500));

export default function App() {
  return (
    <div style={{ padding: 24, fontFamily: "system-ui" }}>
      <Suspense fallback={<p>Loading…</p>}>
        <Greeting namePromise={namePromise} />
      </Suspense>
    </div>
  );
}
05

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.