react.dev icon indicating copy to clipboard operation
react.dev copied to clipboard

[Suggestion]: Document using Promises passed from RSC to the client without `use()`

Open rwalisa opened this issue 8 months ago • 1 comments

Summary

The docs on RSC should mention whether Promises passed from Server Components can be used without use().

Page

https://react.dev/reference/rsc/server-components

Details

Currently, the docs only discuss resolving Promises passed from the server using use(). Sometimes it can be useful to render a component with partial data and update its state once the promise is fulfilled. This appears to work.:

"use client";

export default function Component({ promise }) {
  const [data, setData] = useState(null);
  useEffect(() => {
    let canceled = false;
    promise.then((d) => !canceled && setData(d));

    return () => canceled = true;
  }, [promise])

  return (<p>{data}</p>);
}

However I can't find any mentions of this use case. I think that if this is officially supported, there should be an example showing how to do it. And if not, that should be explicitly stated.

rwalisa avatar May 12 '25 21:05 rwalisa

It seems that if I chain .catch() after .then(), it crashes with Error: promise.then(...) is undefined. However passing the error handler as the second argument to .then() works. Which suggests that this isn't an officially supported API? (I'm using Next 15.3.1)

rwalisa avatar May 12 '25 21:05 rwalisa