Context wrapping in Next.js with Page routing
Answered
Spectacled bear posted this in #help-forum
Spectacled bearOP
Hey Everyone! Can someone please help me out here?
I am creating a simple Next.js application which is a movie review website. So a user can add a movie and add a particular review to a movie. I have created a
The VSCode image contains the code where I am setting the context whenever the card is being clicked. At that point the code works exactly as expected. However when I am trying to do the same thing when accessing the context, it is failing.
I am getting a feeling that I have not wrapped the entire application under context correctly, but I have wrapped
This is the file structure of the code.
I am creating a simple Next.js application which is a movie review website. So a user can add a movie and add a particular review to a movie. I have created a
Movie schema and stored each movie's data in MongoDB. In the home page, all the movies in the db are pulled from backend and displayed there. Now I can click on a single movie card and it will open up the details of the movie in a new page (I am using Next.js page based routing). I can again pull this movie's data from db but it will be an extra call since I have already pulled it to render all the movies. So I am using context to store the movie data in the context when the card is clicked and I want to access this context in the other page to get the movie data. However I am facing this error. Can someone please help me?The VSCode image contains the code where I am setting the context whenever the card is being clicked. At that point the code works exactly as expected. However when I am trying to do the same thing when accessing the context, it is failing.
I am getting a feeling that I have not wrapped the entire application under context correctly, but I have wrapped
layout.tsx within the context. I don't know how to wrap anything further than thisThis is the file structure of the code.
17 Replies
oh I think you are using
useMovie in app/[name]/page.tsx?Spectacled bearOP
No I am not rendering
MovieCard on app/[name]/page.tsx. And yes I have specified 'use client on top of MovieCardUsing
useMovie in app/[name]/page.tsx is causing a problemyes,
useMovie need to be used in client componentSpectacled bearOP
But it is working fine when used in
MovieCardyou should create a client component and render it inside
app/[name]/page.tsx@Ray yes, `useMovie` need to be used in client component
Spectacled bearOP
When I did this it started working. But can you please explain the issue as to why this is happening?
@Ray you should create a client component and render it inside `app/[name]/page.tsx`
Spectacled bearOP
Yes I declared
"use client" at the top of app/[name]/page.tsx and its working now.because hook and context cannot be use in server component
Answer
and by default, all component are server component
unless you put
'use client' on top of the fileSpectacled bearOP
Yes right
So basically anytime I am trying to use a context within a component it has to be a client component?
Spectacled bearOP
Oh okay got it! Thank you so much!!
you are welcome