【发布时间】:2019-05-13 23:08:39
【问题描述】:
有什么方法可以中止 react-native 应用上的 fetch 请求?
class MyComponent extends React.Component {
state = { data: null };
componentDidMount = () =>
fetch('http://www.example.com')
.then(data => this.setState({ data }))
.catch(error => {
throw error;
});
cancelRequest = () => {
//???
};
render = () => <div>{this.state.data ? this.state.data : 'loading'}</div>;
}
我尝试了 AbortController 类中的 abort 函数,但它不起作用!!
...
abortController = new window.AbortController();
cancelRequest = () => this.abortController.abort();
componentDidMount = () =>
fetch('http://www.example.com', { signal: this.abortController.signal })
....
请帮忙!
【问题讨论】:
标签: android react-native fetch