is it possible to pass to children from layout.js some fetched data?
Unanswered
gref9730 posted this in #help-forum
gref9730OP
I would like to create a page where are two columns where on left columns are displayed just names of lines and when you click on it it will append the id to the url and then I would like to render on the right side other component where will be much more detailed info.. but it seems i dont know how to pass the lines to the children so I can acess it in my /[id]/page.js..
import CreateNewView from "./createNewView";
import Line from "./line";
const getData = async () => {
const response = await fetch("http://localhost:3000/api/getDistinctLines", {
cache: "force-cache",
});
if (!response.ok) {
throw new Error("failed to get data...");
}
return response.json();
};
const getLines = async () => {
const response = await fetch(
"http://localhost:3000/api/getPreviewViewArray",
{
cache: "force-cache",
}
);
/* console.log("response...", response);
console.log("response json ...", response.json()) */
if (!response.ok) {
throw new Error("failed to get data...");
}
return response.json();
};
export default async function Layout({ children }) {
const distinctLines = await getData();
const lines = await getLines();
return (
<div className="flex flex-row w-full min-h-screen gap-10">
<div className="flex flex-col w-2/6 rounded-lg bg-green-300 mx-10 my-10 items-center space-y-6">
<CreateNewView distinctLines={distinctLines} />
{lines &&
lines.data.length > 0 &&
lines.data.map((line) => <Line line={line} />)}
</div>
<div className="flex flex-col w-4/6 rounded-lg bg-red-300 mx-10 my-10">
{" "}
{children}
</div>
</div>
);
}46 Replies
Create a layout which contains the left column. Simply use a <Link> component to link to whatever dynamic page you want on click.
The right column will be your page.
gref9730OP
yeah I already got to that point .. the thing is that I want in the page to be able to access the data fetched from the layout
like you would normally prop drill <RandomComponent data={data} /> ...
I need something similar to #Unknown Channel{children data={data}}</>
I need something similar to #Unknown Channel{children data={data}}</>
@gref9730 yeah I already got to that point .. the thing is that I want in the page to be able to access the data fetched from the layout
You can do the fetching again, nextjs actually recommends this
They memoize the response so it's only one request
Lemme show docs
gref9730OP
aaah ok
I was thinking about fetching it again but thought it would be bad bcs any time you would click on different product it would have to re-fetch the data on right..
gref9730OP
i though instead would be better to fetch it once and then prop drill
and if find it
okok, will do that way, thanks !
@gref9730 i though instead would be better to fetch it once and then prop drill
That can be done as well, can you tell me when you are using children?
Instead of just importing the component
gref9730OP
I have layout.js where I fetch the lines data... and then render left colum of lines where you can click it
when you click it it appends to url the id number
and in the same folder I have folder [id]
in that I have [id]/page.js
and I thought that this page will represent the children
so whenever you select some line it will render also the children so the page.js where I would like to render more complex info about the selected line
Yup, correct. In that case you can't really pass data since you are using children.. what you can do is make the layout a page
And inside that have 2 components
gref9730OP
aah
Sidebar and Main
And make that have id as a dynamic param
gref9730OP
that will have to be client component and have useState to hold the id right?
so the other component know which i to filter out
Not really, keep it server side... The page can just get the id as params
And you can pass that id to the 2 components
gref9730OP
uhm oh
ooooh
let me try:D
btw can i somehow force on server side component automatic re-fetch?
when I want to have a page and need to set there re-fetch like every 5 seconds
I tried to add there next: { revalidate : 500}
and it doesnt work..
and it doesnt work..
@gref9730 I tried to add there next: { revalidate : 500}
and it doesnt work..
the revalidate would only work with a router refresh on a cached fetch
you dont need revalidate since its already not storing
@gref9730 when I want to have a page and need to set there re-fetch like every 5 seconds
@Clown couldnt you do this with setInterval calling router.refresh() in a client component?
gref9730OP
is it possible to have server component with automatic refetch
yeah I already tried in client component I wanted to try if its possible also on server component
Oh, well as far as i know.. not really
gref9730OP
oki
maybe supreme might know so wait for him to reply
Can't remember another method directly in NextJs for doing this on server components