Next.js Discord

Discord Forum

Pass props from page to layout

Unanswered
Cape horse mackerel posted this in #help-forum
Open in Discord
Cape horse mackerelOP
Hi team, currently I am trying to pass props (params) from page to layout but it is undefined , how can you pass props to next page

36 Replies

Cape horse mackerelOP
@Cape horse mackerel Click to see attachment
Masai Lion
Are those different files right?
Cape horse mackerelOP
that's right ,
Masai Lion
ContentLayout is which script?
Details/page.tsx?
Cape horse mackerelOP
in file layout.tsx
I want init props for displaying title in confirm page
but currently, it just can pass children from file page.tsx
Masai Lion
Okay so
On your confirm/page.tsx Change it to this:
const ConfirmationPage: NextPage = ({ params }) => {
  return <RegistrationConfirmation title={params.title} />;
};

export default ConfirmationPage;
And then
Create a new document inside where this all are
Call it _app.tsx
Wait @Cape horse mackerel you already have a file called app.tsx or sum?
For this you must handle layout from a file called app.tsx
Cape horse mackerelOP
yah, I just have layout.tsx, not-found,..., I'm trying to avoid using app.tsx, but it's okay if it's necessary to implement.
thank you
You can do this on your layout.tsx
export default function ContentLayout({ children, params }) {
  // Your layout logic here
}
This on layout.tsx
And then
On your confirm/page.tsx
import ContentLayout from '/layout.tsx';

const ConfirmationPage = ({ params }) => {
  return <ContentLayout params={params}>{/* Your page content here */}</ContentLayout>;
};

export async function getServerSideProps() {
  // Fetch your data and return it as props
  const params = {
    title: 'Your Title',
  };

  return {
    props: {
      params,
    },
  };
}

export default ConfirmationPage;
@Cape horse mackerel
@Ray `_app.tsx`, `getServerSideProps` are for page router
Masai Lion
Oh…
Then I’m wrong
@Ray `_app.tsx`, `getServerSideProps` are for page router
Masai Lion
Wait but
Yeah I told them to do it on the page router didn’t I?
@Cape horse mackerel I want init props for displaying title in confirm page
what are you trying to do? why would you need to init props on the layout?
@Masai Lion Yeah I told them to do it on the page router didn’t I?
OP is using app router and you are mixing page router and app router together
@Ray OP is using app router and you are mixing page router and app router together
Masai Lion
Ohhh yeah that’s why
You can pass data to next url using searchParams, then retrieve it in client component that is located inside layout