Next.js Discord

Discord Forum

How to effective share data between server components

Unanswered
American Crocodile posted this in #help-forum
Open in Discord
Avatar
American CrocodileOP
I know that in server components you can basically duplicate the same request and it will be automatically cached.

But what if i need specific data that requires using query params? for example blog post page. What if i would like to fetch main info about post on server and then fetch comments or likes dynamically through another api call and server component?

Since useSearchParams and useContext is client side only i cant pass query params to that server components. i could use props but it end up in big props drilling problem.

So is there any other way to share data between server components?

6 Replies

Avatar
B33fb0n3
if you really don't want to drill the props down, you can create a parallel route for the comments. Then the section is actually a page and the page receives the props that you want @American Crocodile
Avatar
American CrocodileOP
@B33fb0n3 parallel routes seems like a solution but at the same time looks like overengineering for me, especially in case of rendering such small thing as comments to blog post
Avatar
B33fb0n3
I think they are made specifically for that 🙂
Avatar
American CrocodileOP
Well it could be, but in this case i need to create separated layout for every page that requires different server components to render... thats feels way more complex than just defining client component with useSearchParams hook..
Avatar
B33fb0n3
wow yea, thats complex. But I mean only for the comments. You don't need to layout your <Comments> component, because it's not a own page. It's just a part of a page, that gets the params and stuff. So no need for many other files. Just the parallel route and the code inside your blog
Avatar
B33fb0n3
@American Crocodile ?