Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

220
Visualizações
Unable to use getStaticProps in _app.js

I am using next.js and trying to do the following:

  1. Fetch basic data related to user data in _app.js. The data includes a title (for the navbar) and some social links (for the footer).

  2. Pass down that data to other components like Footer and Navbar at the build time (for static site generation).

For that I've exported the getStaticProps from app.js file. But it doesn't seem to work. Here is my app.js.

import Layout from "../src/Layout";
import "../styles/globals.css";
import getAppData from "services/appData";

export default function App({ data, Component, pageProps }) {
  console.log(data, "data"); // data is undefined here
  return (
    <>
      <Layout data={data}>
        <Component {...pageProps} />
      </Layout>
    </>
  );
}

export async function getStaticProps() {
  console.log("working on it dude"); // this message is not logged in console (terminal)
  let data = await getAppData(CLIENTID);
  console.log(data); // data is undefined here as well
  return {
    props: {
      data: data,
    },
    revalidate: 60,
  };
}

And the Layout.js

import React, { useState } from "react";
import Head from "next/head";
import Footer from "./Footer";
import Navbar from "./Navbar";

// Contexts
export const toggleMenu = React.createContext();

export default function Layout({ data, children }) {
  // States
  const [showNav, setShowNav] = useState(true);
  const [showFooter, setShowFooter] = useState(true);

  return (
    <>
      <Head>
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
      </Head>
        <toggleMenu.Provider
          value={{ showNav, showFooter, setShowFooter, setShowNav }}
        >
          {showNav ? <Navbar data={data.navbar} /> : null}
          {children}
          {showFooter ? <Footer data={data.footer} /> : null}
        </toggleMenu.Provider>
    </>
  );
}

data is undefined even if I pass a simple string as props in getStaticProps.


export async function getStaticProps() {
  return {
    props: {
      data: "something",
    },
    revalidate: 60,
  };
}

Knowing the fact that we can't use getStaticProps in a file other than a page, I have to use it in _app.js only and pass it down to <Layout data={data}/> component (if no other solution exists).

I can use getStaticProps in index.js page as well but Navbar and Footer are used in it's parent component i.e, . Any help is appreciated.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

As its already mentioned above _app.js doesn't support getStaticProps or getServerSideProps , you can only use getInitialProps inside _app.js


export default function App({ Component, pageProps }) {
  console.log(pageProps.data, "data"); 
  return (
    <>
      <Layout data={pageProps.data}>
        <Component {...pageProps} />
      </Layout>
    </>
  );
}

App.getInitialProps = async () => {
  let pageProps = {};

  try {
    let data = await getAppData(CLIENTID);
    pageProps["data"] = data;
  } catch (error) {}

  return { pageProps };
};




about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda