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

317
Visualizações
Next.js - API call from different page is not loading the data

Hello everyone I am extremely new at the Next.js world.

I am using the getStaticProps() to make an API call and in order to make it little organized, i have created a separate page "git" under "pages" folder and here is my code:

function Git({ stars }) {
    return <div>Next stars: {stars}</div>
  }

export async function getStaticProps() {
    const res = await fetch('https://api.github.com/repos/vercel/next.js')
    const json = await res.json()
    return { stars: json.stargazers_count }
}

export default Git

And i am trying to load the API data to the index.js file under the "pages" folder.

Inside the index.js file, i am using below to load the API data from the "Git" page

import fetch from "isomorphic-unfetch"
import Git from './Git'

And under the following

render () {
    return (
    <Git />

On the browser, i am not seeing the API data but i am seeing the HTML from the "Git" page

<div>Next stars: </div>

Is there any way if i can load the API data from different page to the index.js page?

However, if i directly access the page for example: http://0.0.0.0:3000/Git then i get the proper API data.

Issue The issue is about the API data in the page "Git" is not getting passed to the main page "index.js" is there any way if i can pass the data from the "Git" to the "index.js"

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

0

I resolved it by putting the following code in the index.js file

const gitData = await Git.getInitialProps();

And that's where I want to print it out

 <Git {...this.props.gitData}/>
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