Open rashidul0405 opened 2 weeks ago
This is because you are trying to use React client hook in a server component. Did you read this? - https://nextjs.org/docs/messages/react-client-hook-in-server-component
Thanks. Yeah, this has been the case for a long time. If I remember correctly, I discovered that use
can be utilized on both server and client components. Additionally, on react.dev, it's documented as an API instead of hooks. https://react.dev/reference/react/use
I'm wondering if I'm correct and what's the proper way to use use
!!
@ElectricCodeGuy it's recommended to use async/await for data fetching?
When fetching data in a Server Component, prefer async and await over use. async and await pick up rendering from the point where await was invoked, whereas use re-renders the component after the data is resolved.
https://react.dev/reference/react/use#caveats
However, this is not answering the issue I am facing.
@eps1lon @delbaoliveira created the issue here from X https://x.com/sebsilbermann/status/1785979252686385321
Does it not compile or does it just silently return the wrong context value? Is it the default that it returns or always an empty object?
It should fail compilation since use(SomeContex)
implies you import a file using createContext
in a server component. This is not supported since React Context is a client-only feature.
You should see an error similar to "createContext only works in Client Components. Add the "use client" directive at the top of the file to use it. Read more: https://nextjs.org/docs/messages/context-in-server-component"
Link to the code that reproduces this issue
https://codesandbox.io/p/devbox/use-use-x3pns4
To Reproduce
pnpm run dev
app/components/button.tsx
Current vs. Expected behavior
while using
use
, context value as a string works fine but context value as an object is not working.String value working fine:
components/theme-provider.tsx
Here theme value is either dark or light, which is a string.
Object value is not working:
components/user-provider.tsx
AND reading value using
use
from context returns empty objectoutputs
Provide environment information
Which area(s) are affected? (Select all that apply)
Not sure, Runtime, Upstream
Which stage(s) are affected? (Select all that apply)
next dev (local), next build (local), next start (local)
Additional context
none of the versions are working including canary. Not sure whether this one is a React issue or Next!!!