In the classic Pages Router of Next, You can add a global provider in your _app.js
file and wrap it around the entire component tree. This will ensure that the provider is available to all components.
If you do this using the App Router, you get an error message
You’re importing a component that needs useState. It only works in a Client Component but none of its parents are marked with "use client", so they’re Server Components by default.
Is there a way to achieve something similar without giving up the Server Components?
2
Answers
// use context in _app.js
// user.js
// _app.js
// use user state in any component like this
Wrap the provider in a client component.
And use this wrapped provider in root layout.