Q38: How does the new use() Hook operate in React 19?
The use() Hook in React 19 reads the value of a resource like a Promise or Context directly inside render execution. Unlike standard Hooks, use() can be called conditionally inside if statements and loops.
Conditional Promise Unwrapping with use()
import { use } from 'react';
function Comments({ commentsPromise, showComments }) {
if (!showComments) return null; // Conditional execution permitted!
// Unwraps promise value directly; integrates with Suspense
const comments = use(commentsPromise);
return <ul>{comments.map(c => <li key={c.id}>{c.text}</li>)}</ul>;
}use() unwraps promises inside render methods and can safely execute conditionally.
Key Takeaways
Unwraps promises and context resources inside render phases.
Allowed inside conditional statements and loops unlike standard Hooks.
Knowledge Check
Is the React 19 use() Hook permitted inside IF conditionals?