我想每1000毫秒更新一次React组件的状态。但是,我试图在componentDidMount
上做setInterval
,但没有运气。目前,我在我的console.log
中得到了两个结果,一个是构造函数中的空状态对象,另一个是来自API的获取对象。如何使用setInterval每1000毫秒更新组件的状态?在React中将setInterval添加到componentDidMount中
这是我的代码:
let url = 'some-link-bla-bla';
class Basemap extends React.Component {
constructor(props) {
super(props);
this.state = {};
console.log(this.state);
}
render() {
return (
<Scene style={{ width: '100vw', height: '100vh' }}
mapProperties={{ basemap: 'satellite' }}
viewProperties={ this.state } />
);
}
componentDidMount() {
fetch(url)
.then(d => d.json().then(function(d) {
console.log(d);
}))
.then(d => function(d) {
this.setState({
center: [
{latitude : d.iss_position.latitude} + ', ' +
{longitude: d.iss_position.longitude}
]
})
});
}
}
export default Basemap;
你能不能用'setInterval'添加你尝试的代码。您的代码没有 – atomrc
@ IsaaK08,如果您在此找到正确的解决方案,请将其设置为已接受。 –