Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/470.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript React中闪烁速度不一致_Javascript_Reactjs_Ecmascript 6_Blinker - Fatal编程技术网

Javascript React中闪烁速度不一致

Javascript React中闪烁速度不一致,javascript,reactjs,ecmascript-6,blinker,Javascript,Reactjs,Ecmascript 6,Blinker,我试图在React中创建一个闪烁器,但是,闪烁器本身以不一致的速率闪烁/渲染,行为有些令人沮丧。请参阅所附的代码片段。部件安装是否有问题?谢谢 类闪烁器扩展React.Component{ 建造师(道具){ 超级(); 此.state={ 出现:正确 } this.blinker=this.blinker.bind(this); } 闪烁器(){ this.setState({seen:!this.state.seen}); } componentDidMount(){ 设置时间间隔(此.bl

我试图在React中创建一个闪烁器,但是,闪烁器本身以不一致的速率闪烁/渲染,行为有些令人沮丧。请参阅所附的代码片段。部件安装是否有问题?谢谢

类闪烁器扩展React.Component{
建造师(道具){
超级();
此.state={
出现:正确
}
this.blinker=this.blinker.bind(this);
}
闪烁器(){
this.setState({seen:!this.state.seen});
}
componentDidMount(){
设置时间间隔(此.blinker,1000)
}
componentDidUpdate(){
setTimeout(this.blinker,1000)
}
render(){
返回(
{(this.state.appease)&&“xxx”}
);
}
}
ReactDOM.render(
,
应用程序
);

删除
组件diddupdate
。为什么要在组件更新后触发新的1秒超时?从
didMount
开始的间隔仍将运行

此外,您需要清除组件卸载时的间隔:

componentDidMount() {
  this.blinkerId = setInterval(this.blinker, 1000)
}

componentWillUnmount() {
  clearInterval(this.blinkerId);
}

删除
组件diddupdate
。为什么要在组件更新后触发新的1秒超时?从
didMount
开始的间隔仍将运行

此外,您需要清除组件卸载时的间隔:

componentDidMount() {
  this.blinkerId = setInterval(this.blinker, 1000)
}

componentWillUnmount() {
  clearInterval(this.blinkerId);
}