2017-07-19 44 views
0

我怎麼能取消的時候分量離開(卸載)中止取時組件卸載

例如讀取操作:用戶打開此屏幕和前回去取完整

這裏是我做了什麼

export default class LecturesScreen extends React.Component 
{ 
     constructor(props){ 
     super(props); 
     this.state = {lectures : [] , loadingNow : true} 
    } 

    componentDidMount(){ 
     this.loadLectures(); 
    } 

    loadLectures =() =>{ 
     fetch(Link.api.lecture.newest , {method : 'POST'}) 
      .then((response) => 
      { 
       this.setState({lectures : response , loadingNow: false}); 
      }) 
      .catch((error)=> 
      { 
       console.log(error); 
      }); 
    }; 

    render(){ 
     return (
      <View> 

       <List dataArray={this.state.lectures} 
         renderRow={(item) => <LectureListItem title={item.Title}/>} 
       /> 

      </View> 
     ); 
    } 
} 

回答

1

編輯:

我不認爲這是目前可能取消/放棄獲取請求。 您可以按照這裏的討論:

https://github.com/whatwg/fetch/issues/447

調用一個函數上componentDidUnmount類似下面。

componentWillUnmount(){ 
    this.unLoadLectures(); 
} 

unLoadLectures =() =>{ 
    this.state = {lectures : [] , loadingNow : true} 
}; 
+0

我認爲這將不會中止'獲取'功能 – Ali

+0

看看這篇文章。 https://discuss.reactjs.org/t/cancel-async-fetch-on-unmount/3272 – Etherealm

+0

感謝您的重播 – Ali