Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

134
Views
How pass dynamic props in next js persistance layout

for a persistence layout, I use getLayout function on every page like this,

import { Layout } from 'hoc';

const Page = () => {
  return (
    <div>hii</div>
  );
};

Page.getLayout = function getLayout(page: React.ReactNode) {
  return <Layout>{page}</Layout>;
};

export default Page;

Now I want to pass some dynamic data through the props in Layout, and the data will come inside Page component through an API call, but it is not possible because we are setting the layout after Page component is made.

import { Layout } from 'hoc';
import { fetchData } from 'api/index';
import { useQuery } from 'react-query';

const Page = () => {
const { data } = useQuery('data', fetchData);
// I want to pass this data to the Layout component

return ( <div>hii</div>);
};

Page.getLayout = function getLayout(page: React.ReactNode) {
  return <Layout>{page}</Layout>;
  // <Layout data={data}>{page}</Layout>
  // this data should come from the Page component and pass through layout
};

export default Page;

I tried to make the solution with context but my provider should be a top-level component, and for that, the solution is not working for this scenario, also saving a component in a global state does not seem like a good idea. I appreciate any kind of help, Thanks in advance _ / \ _

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

you can do this at your page component:

Page.getInitialProps = () => {
  return {
    data: 'your data',
  }
}

and at layout component:

function getLayout(page) {
  const { props } = page
  return (
    <YourLayout data={props.data}>
      {page}
    </YourLayout>
  )
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!