【发布时间】:2021-10-02 03:44:49
【问题描述】:
我正在尝试让以下内容每 30 秒重新加载一次。
componentDidMount() {
fetch('https://example.com/json')
.then((res) => res.json())
.then(
(result) => {
this.setState({
isLoaded: true,
items: result.data,
});
},
// Note: it's important to handle errors here
// instead of a catch() block so that we don't swallow
// exceptions from actual bugs in components.
(error) => {
this.setState({
isLoaded: true,
error,
});
},
);
}
我尝试将其包装在 setInterval 中,但没有成功。我想知道如何将它变成一个函数,以便我可以根据需要重新加载它。
【问题讨论】:
-
我认为您的问题类似于:stackoverflow.com/questions/45982244/…
-
componentDidMount在渲染阶段只触发一次,而且 setState 是异步的。 -
我想你可以在 react 文档中找到示例 ==>reactjs.org/docs/…
标签: reactjs next.js settimeout setinterval