Repository navigation
Best pattern for sharing server-fetched data across multiple Client Components in App Router? #98240
Replies: 2 comments
|
Hi, I’d separate server-side deduplication from client-side sharing. For this case, a good pattern is:
So, roughly: const data = getData()
return (
<DataProvider data={data}>
<ClientTree />
</DataProvider>
)
I’d avoid refetching the same data from each Client Component just to avoid prop drilling. |
|
A good default is to fetch the data in the Server Component and pass only the serializable data each Client Component actually needs. If several deeply nested Client Components need the same data, a Client Context provider can be useful. In that case, I would initialize the provider with the server-fetched data instead of fetching the same data again on the client.
So a practical rule of thumb is:
This keeps duplicate fetching and unnecessary client-side JavaScript to a minimum. |
Uh oh!
There was an error while loading. Please reload this page.
Summary
In the App Router, suppose a Server Component fetches data that several nested Client Components need.
Additional information
Example
No response
All reactions