Loading...
'use client';
function Comments({ postId }) {
const comments = use(fetch(`/api/posts/${postId}`).then((r) => r.json()));
return <List items={comments} />;
}'use client';
function Comments({ commentsPromise }) {
const comments = use(commentsPromise);
return <List items={comments} />;
}The promise is created once on the server and passed as a prop, so it stays the same across renders instead of suspending again.
fetch promise inside a Client Component, which makes it suspend forever on every render.use() come from a Server Component, a cache or a library, not created during client render.<Suspense> and an error boundary.use() is not wrapped in try/catch, errors are handled by the error boundary.Yes. Unlike hooks, use() can be called in conditions and loops, but it must still be called inside a component or a hook.
Both read context. use(Context) also works after an early return or inside a condition, which useContext does not allow.
Usually not. In Server Components, async and await are simpler. use() is mainly for reading a promise in a Client Component.