Next.js Discord

Discord Forum

Updating a context higher up in the tree on page load

Answered
Cuckoo wasp posted this in #help-forum
Open in Discord
Cuckoo waspOP
Hey, I am just trying to apply some values to a context from within a layout file for a sub page to a context provided at the root of the app but I am getting the error in the attached image.
I understand that there are some timing issues with what I am trying to do, but is there an alternative way I could set the contents of this 'global' context when this specific layout file is rendered?
I have tried to include the update in the page file to the same effect.
Very much a beginner with React and NextJS so sorry if some terminology is off! Appreciate any advice and alternatives. Long term I am thinking the context would be populated from a DB call based on the path to return the correct info but as I said just trying to put something basic in place and struggling :(
Answered by Cuckoo wasp
Solved it for now, I used useEffect
View full answer

2 Replies

Cuckoo waspOP
Is there anything more I could add to my OP that would be helpful? the Context holds the state variable for PageState, so maybe the problem is with state and not context? Should I make the update a function passed to setstate? I am just looking for some advice on a possible working approach
Cuckoo waspOP
Solved it for now, I used useEffect
Answer