DEV Community

Discussion on: Using React Context in NextJS Server Components

Collapse
 
sney profile image
Snehil

If the parent of a server component is a provider, which is a client component; would it benefit from being a server component?
e.g.
in Parent > ChildA > ChildB('use client'), parent and childA can benefit from server components.
by doing the above, Parent > ContextProvider('use client') > ChildA > ChildB('use client'), does ChildA benefit from being a server component as its wrapped in a client component..

Collapse
 
mistersingh179 profile image
Mister Singh

yes ChildA in your example does benefit. this is because if ChildA is a server component, it is still being built on the server and gets all benefits of sever side rendering like fetch memoization, data cache, & full route cache.
with that said it is import to know how ChildA was put in the parent context provider client component. it needs to go in as part of the children. this means that it is not being imported in childA, but rather being passed in from above as a prop. this is what allows it to be server side rendered.
there is so important that they even named this pattern. It is called the interleaving pattern.
in general you can benefit by reading this part of the documentation
nextjs.org/docs/app/building-your-...