[英]How to refetch api data on client side in next.js?
I need to reload the data from api once state changed. 状态更改后,我需要从api重新加载数据。 Is is possible to fetch data again with getInitialProps on client side? 是否可以在客户端使用getInitialProps再次获取数据?
Why don't you fetch the data on the componentDidMount? 您为什么不获取componentDidMount上的数据?
componentDidMount() {
yourAPI.fetch().then((pFetchData) => {
this.setState({data: pFetchData});
});
}
In practice, componentDidMount is the best place to put calls to fetch data, for two reasons: 实际上,出于两个原因,componentDidMount是发出调用以获取数据的最佳位置:
https://www.codeproject.com/Articles/1177776/Where-to-Fetch-Data-componentWillMount-vs-componen https://www.codeproject.com/Articles/1177776/Where-to-Fetch-Data-componentWillMount-vs-componen
Hope it helps :) 希望能帮助到你 :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.