Search code examples
javascriptreactjsserver-side-renderingnext.js13react-state-management

How to use Context API in Next JS 13 with server-side components?


Is it possible in Next JS 13 to fetch data on the server but then propagate it to Context API?

In my case, I want to fetch user information via token on the server, but then pass it to Context API since I may use user data somewhere else in the app. The problem is that Context API is a client-side technology, so wherever I want to use it I end up declaring client components with 'use client'. Is it possible to get the best of two worlds?


Solution

  • It is not possile to use context with ServerSide component. You can use zustand for creating global state that does not require elements to be wrapped inside a provider.