getInitialProps does not work when the page reload

不羁岁月 提交于 2021-01-03 06:20:19

问题


I'm using getInitialProps in the _app.js component, but when I reload the page, the request does not execute.

For example:

// getData.js
import * as axios from 'axios';
export default async function getData() {
  const response = await axios.get('http://someapi.com/');

  return response.data;
}

And then I'm going to use that data...

// _app.js
import getData from './getData';
import App, { Container } from "next/app";

class MyApp extends App {
  static async getInitialProps() {
    const response = await getData();

    if (response) {
      return { response };
    }
    return {};
  }
  render() {
    const { Component, response } = this.props;
    <Container>
      <Component {...this.props} data={response} />
    </Container>
  }
}

The first time, it works perfectly, but when I reload the page the getInitialProps() function does not executes :(

How can I resolve this?

Thank you.

来源:https://stackoverflow.com/questions/52241387/getinitialprops-does-not-work-when-the-page-reload

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!