#Passing promises from server to client components.

1 messages · Page 1 of 1 (latest)

warm parcel
#

I've been trying to use the pattern stated on react docs (https://react.dev/reference/react/use#streaming-data-from-server-to-client) and also next docs (https://nextjs.org/docs/app/getting-started/fetching-data#client-components), to start promises on the server and pass as prop to client components resolve.

The issue is that it just block render, so I'm not sure if I understood how to achieve this without blocking render on the server.

page.tsx


import PromiseTest from '@/components/PromiseTest'

export default function Page() {
  const promise = fetch('https://httpbin.org/delay/5').then((res) => res.json())

  return (
    <div>
      <Suspense fallback={<p>loading...</p>}>
        <PromiseTest promise={promise} />
      </Suspense>
    </div>
  )
}

PromiseTest.tsx


import { use } from 'react'

export default function PromiseTest({ promise }: { promise: Promise<any> }) {
  const test = use(promise)
  return <div>PromiseTest</div>
}

"next": "16.0.1"
"react": "19.2.0"

The library for web and native user interfaces

Learn how to fetch data and stream content that depends on data.

onyx thornBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize

✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)

heavy jay
#

Lgtm what do you mean block render? It should show the fall back until the promise resolves