【发布时间】:2018-05-14 15:17:20
【问题描述】:
我正在使用setState() 更新显示用户未读消息数量的徽章:
updateUnread(){
this.setState({
unreadCount: Math.floor(Math.random() * 100)
});
}
render(){
setInterval(() => this.updateUnread(), 2000);
return (
<div className="wrapper">
<Messages unreadCount={this.state.unreadCount} />
</div>
);
}
但是,它一直在数字之间闪烁,正如您在 this video 中看到的那样。我不确定为什么会这样,因为我对 React 还很陌生,但我认为可能是每次更新时都会创建一个新的间隔。如果是这种情况,我该怎么做?
是的,我知道它只是放在那里的随机数,这只是开发:)
【问题讨论】:
-
我也找到了解决方案。不过,感谢您的帮助。