function Note({ show, notePromise }) {
if (show) {
const note = use(notePromise);
return <p>{note.text}</p>;
}
return null;
}
This is valid;
use can be called conditionally, and the component suspends until the promise resolvesThe code is valid. Unlike hooks, use can be called inside conditions and loops. It must still be called only in components or hooks. It reads context or a promise. With a promise, the component suspends: it pauses and shows the nearest Suspense fallback until the promise resolves.
"This is invalid" is the tempting wrong answer, because use looks like a hook. But it is the one exception to the rules of hooks. use reads promises as well as context. The try/catch option is also wrong: use cannot be called inside try/catch. Handle rejections with an error boundary or a .catch on the promise.
Read more