2

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?

Mayank Kumar Chaudhari
  • 16,027
  • 10
  • 55
  • 122
KamranV21
  • 53
  • 7
  • Did you end up finding a solution? I'm just starting to learn NextJS and ran into this question. – robert theboss Aug 10 '23 at 11:41
  • There is no way to pass data from client to server with Context API. But you can try zustand as was mentioned by Mayank Kumar Chaudhari. It's a super easy to use state management system, you should definitely give it a try. – KamranV21 Aug 28 '23 at 07:41

1 Answers1

1

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.

Mayank Kumar Chaudhari
  • 16,027
  • 10
  • 55
  • 122