簡體   English   中英

React Native,無法定期更新組件

[英]React Native , Unable to update component periodically

我是剛開始學習反應原生的新手。
這是我嘗試過的

 export default class HomeComponent extends React.Component {
 constructor(props) {
 super(props);
 this.state = {
  item: [],
 };
 mixins: [TimerMixin];

 }
 componentDidMount(){

this.interval = setInterval(() => {
console.log('hi');
  this.setState({item: updateValue})
 }, 6000); //6 seconds
 }

 render() {
return <View style={{ flex: 1, backgroundColor: '#101010' }}>

</View>;
 }

它工作正常,但我只是在考慮警告,我應該忽略它嗎? 或者有更好的方法嗎!。

我收到此警告 計時器日志

即使在組件卸載導致錯誤后,您的函數也會繼續更新。 你應該使用 React 生命周期的 componentWillUnmount() 函數,並在組件不再掛載時清除“setInterval”函數的運行。

您可以使用:

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

卸載組件后關閉更新。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM