How to pass data from layout to page #59488
Replies: 3 comments 1 reply
-
I tried solving this using React cache, but I'm not sure if this is the correct approach. |
Beta Was this translation helpful? Give feedback.
-
I know this post is old, but I spent a LOT of time figuring this out, so I’d like to share it for anyone who still needs a solution. My goal was simple: I have a Layout Server that is the parent of a Page Client, and I wanted the Layout Server to fetch data and pass it to the Page Client (preferably using Summary:
Structure:
Layout (Server):
Context and Component (Client):
Page 1 (Client):
Page 2 (Client):
That’s it! I’m using Personally, I use JWT in cookies instead of params. Here’s a version using them:
Hope this helps someone! |
Beta Was this translation helpful? Give feedback.
-
I also used the context method after initially trying the headers method without success. Here's an example: 📝 Project Structure: 1/We create a React context to share data. 📄 UseLayoutData.js
2/ Store Data in layout.js (Server) 📄 layout.js
3/Create a Client Component that Uses the Context 📄 ChildComponent.js
4/ Display the Component in page.js 📄 page.js
I used this hybrid client/server technique, which works in my project. I hope good luck to everyone! |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Summary
Hi there! There is a section User Profile where I get username from URL. Then in layout I make a request to API passing this username and receive UserID + extra user info. I render profile header in layout using user info. But I also need to pass UserID to the page for other API requests.
{children}
usingReact.cloneElement
doesn't workThanks in advance.
Additional information
Example
No response
Beta Was this translation helpful? Give feedback.
All reactions