Skip to main content

use

use is a polyfill hook to resolve promises state for React v18 and below. Note that it only implements the ability to consume promises.

Usage

Live Editor
const promise = new Promise((resolve) => {
  setTimeout(() => {
    resolve("resolved");
  }, 5000);
});

function Demo() {
  const data = use(promise);

  return <p>{data}</p>;
}

render(
  <Suspense fallback="loading...">
    <Demo />
  </Suspense>
);
Result
Loading...

API

Use

Returns

T: resolved state value

Arguments

ArgumentDescriptionTypeDefaultValue
usablepromise or contextUsable<T> (Required)-

Usable

type Usable<T> = Thenable<T> | Context<T>;