And*_*oya 5 node.js reactjs 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;
}
Run Code Online (Sandbox Code Playgroud)
然后我将使用这些数据......
// _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>
}
}
Run Code Online (Sandbox Code Playgroud)
第一次,它工作得很好,但是当我重新加载页面时, getInitialProps() 函数不执行:(
我该如何解决这个问题?
谢谢。
| 归档时间: |
|
| 查看次数: |
1460 次 |
| 最近记录: |