Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

102
Vistas
How can I access ...pageProbs that I passed to a Component in _app.js

I am loading a bunch of data at the loading of my site via getInitialProps.

MyApp.getInitialProps = async (appContext) => {
// Calls page's `getInitialProps` and fills `appProps.pageProps`
const appProps = await App.getInitialProps(appContext)
const globalLocale = await getGlobalData()

return {
  ...appProps,
  pageProps: {
    global: globalLocale,
  },
}

The return statement.

return (
        <>
        <Head>
            <link rel="shortcut icon" href={getStrapiMedia(global.favicon.url)} />
        </Head>
        <Layout>
            <Component {...pageProps} />
        </Layout>
        </>
    )

My question now is how I can access the pageProbs on a different page. For example I want to access the data I loaded in the _app.js on my index.js page. However I am absolutely clueless how I can make use of them.

Cheers.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

For anyone wondering what I was actually looking for since it might be helpful:

The solution was actually quite simple.

As described I have a simple _app.js file looking like this:

const MyApp = ({ Component, pageProps }) => {

    // Extract the data we need
    const { global } = pageProps
    if (global == null) {
        return <p>404</p>
    }

    return (
       <>
        <Layout>
            <Component {...pageProps} />
        </Layout>
       </>
    )
}

MyApp.getInitialProps = async (appContext) => {
    // Calls page's `getInitialProps` and fills `appProps.pageProps`
    const appProps = await App.getInitialProps(appContext)
    const globalData = await getGlobalData()
  
    return {
      ...appProps,
      pageProps: {
        global: globalData,
      },
    }
  }

export default MyApp;

The goal of my question was to be able to access the ...pageProbs in my index.js file.

The solution after reading the docs was quite simple. I just needed to define the pageProbs as a parameter in my const IndexPage;

const IndexPage  = (pageProps) => {

    global = pageProps.global;

    return (
        <>
            <p>{global.id}</p>
        </>
    )
}

export default IndexPage;

simple as that. I do feel a little dumb now for asking it. Still was just not sure how to phrase my question, I think that was the biggest problem

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda