页面重新加载时,getInitialProps不起作用

时间:2018-09-09 04:39:05

标签: node.js reactjs next next.js

我正在_app.js组件中使用getInitialProps,但是当我重新加载页面时,请求不会执行。

例如:

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

  return response.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>
  }
}

第一次,它运行良好,但是当我重新加载页面时,getInitialProps()函数不会执行:(

我该如何解决?

谢谢。

0 个答案:

没有答案