passing props to server comps from server comp
Unanswered
Peterbald posted this in #help-forum
PeterbaldOP
howc an this be done
44 Replies
im not sure if i understand... you can pass them like react components could always...
PeterbaldOP
the thing is i need to do it as children
so map over children
and apply it to to each and it doesn't work
can you show what "didn't work"
PeterbaldOP
import {Sidebar} from "@/app/Sidebar";
import React from "react";
interface IAuthorizedPage {
children: React.ReactNode
}
async function AuthorizedPage(props: IAuthorizedPage) {
return (
<main className="flex w-screen h-screen bg-zinc-800">
<Sidebar></Sidebar>
<div className={"grow"}>
{React.Children.map(props.children, (child: React.ReactNode) => {
return React.cloneElement(child, {
name: "ch",
} as any);
})}
</div>
</main>
)
}
export default AuthorizedPage;import './globals.css'
import React from "react";
import AuthorizedPage from "@/app/AuthorizedPage";
// export const metadata = {
// title: 'Create Next App',
// description: 'Generated by create next app',
// }
interface IRootLayoutProps {
children: React.ReactNode
}
export default function RootLayout(props: IRootLayoutProps)
{
return (
<html lang="en">
<body>
<AuthorizedPage>{props.children}</AuthorizedPage>
</body>
</html>
)
}export async function Page(props: any) {
return (
<h1 className={"grow text-3xl text-white"}>Props: {JSON.stringify(props)}</h1>
)
}
export default Page;the h1 here is not showing the a prop
What are you doing with React.Children
It's just props.children.map directly
PeterbaldOP
Error: props.children.map is not a function
@Peterbald js
import {Sidebar} from "@/app/Sidebar";
import React from "react";
interface IAuthorizedPage {
children: React.ReactNode
}
async function AuthorizedPage(props: IAuthorizedPage) {
return (
<main className="flex w-screen h-screen bg-zinc-800">
<Sidebar></Sidebar>
<div className={"grow"}>
{React.Children.map(props.children, (child: React.ReactNode) => {
return React.cloneElement(child, {
name: "ch",
} as any);
})}
</div>
</main>
)
}
export default AuthorizedPage;
I'm not really sure what you're doing here or why. Why are you mapping children? Why are you using React.cloneElement? Authentication should be handled in middleware, not with context on your root layout.
PeterbaldOP
midddleware? can you explain this a bit more?
i know what middleware is
PeterbaldOP
ah
i didn't know you could persit state in middleware
PeterbaldOP
will this still be fine
if i use rust jwts to authenticate
yes
There's some extra config with jwts - you should treat next like an independent microservice
@Peterbald if i use rust jwts to authenticate
Are you using rust just for auth?
PeterbaldOP
yes pretty much,rust and postgres for everything
@Peterbald yes pretty much,rust and postgres for everything
Are you using a rust API? Just curious as to why, if there is a reason
PeterbaldOP
well my entire backend is rust does that clear it up?
imo using nextjs but with seperate backend kinda removes most of its purpose... (but thats unrelated here)
PeterbaldOP
how so?
is it to do with the lateny between server side requests when they are hosted on the same server?
is it to do with the lateny between server side requests when they are hosted on the same server?
I just like rust's saftey
its just server rendering and nextjs is designed to be the full stack framework and not half, not to say it won't work but its prob harder
@Peterbald how so?
is it to do with the lateny between server side requests when they are hosted on the same server?
It's more of having 2 backends, makes sense if you have a bunch of other services connecting to it but wasn't sure if you were using it just for your Next.js app
PeterbaldOP
well why can't i enjoy the benefits of server side rendering while also the enjoying what rust brings
@Peterbald well why can't i enjoy the benefits of server side rendering while also the enjoying what rust brings
I mean, you can if you want. I was just curious if it was like a preference or related to other parts of your infra
PeterbaldOP
its just a preference
so can i still use auth js
should i?
@Peterbald should i?
No reason not to, really. It's recommended
Alternative would be checking your headers manually
PeterbaldOP
i use token based auth not session
is that ok with auth js
@Peterbald is that ok with auth js
yes, session is still token, just a different type
PeterbaldOP
hey are you still here?