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

2023-12-29

我在 _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() 函数不执行:(

我该如何解决这个问题?

谢谢。


None

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

页面重新加载时 getInitialProps 不起作用 的相关文章

随机推荐