0

In this example from react docs, I can remove use hook from const messageContent = use(messagePromise); leaving it like this const messageContent = messagePromise; and it still works (no error is thrown, suspense is triggered). The only difference is that use suspends component's rendering, while without use it re-renders various times until the promise gets resolved.

This is weird. How does react works when you don't use use hook here? Where can I read about this?

function Message({ messagePromise }) { const messageContent = use(messagePromise); // !!! weird. this still works, only re-renders more times. // const messageContent = messagePromise; return <p>Here is the message: {messageContent}</p>; } export function MessageContainer({ messagePromise }) { return ( <Suspense fallback={<p>⌛Downloading message...</p>}> <Message messagePromise={messagePromise} /> </Suspense> ); } 
3
  • It would help if you linked to the React docs that you're referring to. Commented Nov 2 at 8:32
  • 1
    This is a known undocumented behaviour Commented Nov 2 at 9:29
  • React has to parse and understand the JSX tree your component is returning. And it will contain a promise and it can detect that. I don't have the necessary knowledge to explain the use function(use is not really a hook), but maybe this will help you in your quest, learn a bit more about how react rendering works. kentcdodds.com/blog/optimize-react-re-renders Commented Nov 2 at 21:06

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.