use
Reads the value of a resource such as a Promise or context.
Syntax
const value = use(resource) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
resource | Promise | Context | Yes | A Promise to suspend on, or a context object to read. |
Returns
any — The resolved promise value or the current context value.
Examples
function Comments({ commentsPromise }) {
const comments = use(commentsPromise);
return comments.map(c => <p key={c.id}>{c.text}</p>);
}
function Themed() {
const theme = use(ThemeContext);
return <div className={theme} />;
}
Notes
Unlike other hooks, `use` may be called conditionally and inside loops, but
still only within a component or hook. Reading a Promise suspends the
component until it resolves, so wrap it in `<Suspense>`. Promises should be
created in a Server Component or cached, not created during render on the
client.