Next.js Discord

Discord Forum

Won't createContext() make every child component client sided?

Answered
Savannah posted this in #help-forum
Open in Discord
SavannahOP
I'm new to NextJS and SSR is a pretty unknown thing for me right now. I'm creating auth for my app and right now I need to wrap every project component in context provider. I cannot create the provider without using "use client". Won't it make every child component client sided?
Answered by Rafael Almeida
the solution is to create the context provider on its own file using 'use client' and wrap the app with it: https://nextjs.org/docs/app/building-your-application/rendering/composition-patterns#using-context-providers
as explained in this doc page, a server component rendered by the children of a client component does not change to be a client component, so the rest of the app continue being considered as server components
View full answer

2 Replies

the solution is to create the context provider on its own file using 'use client' and wrap the app with it: https://nextjs.org/docs/app/building-your-application/rendering/composition-patterns#using-context-providers
as explained in this doc page, a server component rendered by the children of a client component does not change to be a client component, so the rest of the app continue being considered as server components
Answer
SavannahOP
Oh, I see! Thanks, this helps