2017-07-19 45 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