2017-09-10 21 views
2

コード:私の他の関数が呼び出された場合自分自身を呼び出そうとすると、反応コンポーネントが複数回レンダリングされますか?

myMethod =() => { 
setTimeout(()=> { 
if(!this.state.otherFuncHasBeenCalled) { 
    this.myMethod() 
} 
},5000) 
} 

はそうotherFuncHasBeenCalled状態がtrueに設定されています。私がここでやりたかったのは、myMethodが呼び出され、5秒後に他の関数が呼び出されずにotherFuncHasBeenCalledの状態がtrueに設定されている場合は、myMethodが再び呼び出されます。しかし、コンポーネントを複数回レンダリングします。助けて?

答えて

0

どうやってこのようなことができますか?

myMethod =() => { 
    setTimeout(()=> { 
    if(!this.state.otherFuncHasBeenCalled) { // wait 5 sec for check 
    this.setState(
     {otherFuncHasBeenCalled: true}, // update state 
     this.myMethod // run again after state has been set 
    } 
    },5000) 
} 
関連する問題