Ant-design-pro: 请问如何在组件中使用计时器定时从API获取数据?

Created on 28 May 2020  ·  2Comments  ·  Source: ant-design/ant-design-pro

🧐 问题描述

我想在页面中使用计时器,每三秒像服务器请求一次数据并更新表格,我一开始是在页面中使用,但是发现this.setState只能设置页面的数据而不能单独去设置Table的数据,于是我新建了一个文件夹然后在组件里修改(我使用的TableBasic)但是组件里没有setState。

💻 示例代码

·
var dataSource = [];
class Result extends React.Component{
constructor(props) {
super(props);
// Don't call this.setState() here!
this.state = { data: dataSource };
//this.handleClick = this.handleClick.bind(this);
}
componentDidMount(){
/this.timerID = setInterval(()=>{
console.log("Fetch result...")
request('http://localhost:8081/api/result/').then(data=>{
//console.log(data);
dataSource.push(data);
//console.log(dataSource);
this.setState({
dataSource: dataSource
})
})
}, 3000);
/
}
`
编译器报错:
Property 'timerID' does not exist on type 'VideoResult'.ts(2339)

🕵🏻‍♀️ question

Most helpful comment

All 2 comments

我重新编译了下,这回虽然编译器报错,但是运行的时候没有报错,但是我目前仍然无法做到每隔3秒让表格组件自己重新渲染。

Was this page helpful?
0 / 5 - 0 ratings

Related issues

skyFi picture skyFi  ·  3Comments

Yoping picture Yoping  ·  3Comments

zhuanglong picture zhuanglong  ·  3Comments

lvzheng0404 picture lvzheng0404  ·  3Comments

yadongxie150 picture yadongxie150  ·  3Comments