在react-native中取消获取请求

时间:2019-05-13 23:08:39

标签: android react-native fetch

有什么方法可以中止react-native应用程序上的提取请求吗?

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>;
}

我尝试了abort类中的AbortController函数,但是它不起作用!

...
abortController = new window.AbortController();

cancelRequest =  () => this.abortController.abort();

componentDidMount = () =>
        fetch('http://www.example.com', { signal: this.abortController.signal })
          ....

请帮助!

4 个答案:

答案 0 :(得分:1)

您实际上可以通过安装此polyfill abortcontroller-polyfill来实现 这是取消请求的快速示例:

import React from 'react';
import { Button, View, Text } from 'react-native';
import 'abortcontroller-polyfill';

export default class HomeScreen extends React.Component {
  state = { todos: [] };

  controller = new AbortController();

  doStuff = () => {
    fetch('https://jsonplaceholder.typicode.com/todos',{
      signal: this.controller.signal
    })
    .then(res => res.json())
    .then(todos => {
      alert('done');
      this.setState({ todos })
    })
    .catch(e => alert(e.message));
    alert('calling cancel');
    this.controller.abort()
  }


  render(){
    return (
      <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
        <Text>Details Screen</Text>
        <Button title="Do stuff" onPress={() => { this.doStuff(); }} /> 
      </View>
    )
  }
}

因此,基本上在此示例中,一旦单击“ doStuff”按钮,请求将立即被取消,并且您永远不会收到“完成”警报。可以肯定的是,请尝试对这些行进行注释,然后再次单击按钮:

alert('calling cancel');
this.controller.abort()

这一次您将收到“完成”警报。

这是一个简单的示例,您可以在react native中使用fetch取消请求,随时将其用于您自己的用例。

以下是小吃博览会https://snack.expo.io/@mazinoukah/fetch-cancel-request上的演示的链接

希望它会有所帮助:)

答案 1 :(得分:1)

最好的解决方案是使用rxjs observables + axios / fetch而不是promises,中止一个请求=>取消订阅一个observable:

import Axios from "axios";
import {
    Observable
} from "rxjs";

export default class HomeScreen extends React.Component {
    subs = null;

    doStuff = () => {
        let observable$ = Observable.create(observer => {
            Axios.get('https://jsonplaceholder.typicode.com/todos', {}, {})
                .then(response => {
                    observer.next(response.data);
                    observer.complete();
                })
        });

        this.subs = observable$.subscribe({
            next: data => console.log('[data] => ', data),
            complete: data => console.log('[complete]'),
        });

    }

    cancel = () =>
        if (this.subs) this.subs.unsubscribe()

    componentWillUnmount() {
        if (this.subs) this.subs.unsubscribe();
    }

}

就是这样:)

答案 2 :(得分:1)

我实际上已经写了很多关于这个主题的文章。 您还可以找到第一个问题,该问题是我here

在React Native中打开的 OLD 缺少AbortController的问题

支持降落在RN 0.60.0 中,您可以在我的博客an article about thisanother one that will give you a simple code上找到,以开始使用React发出可中止的请求(以及更多内容)。也是本地人。它还为不支持的环境(例如RN <0.60)实现了一个小的polyfill。

答案 3 :(得分:0)

您不再需要任何polyfill来中止React Native 0.60 changelog

中的请求

这是来自doc条的“ react-native”示例:

/**
 * Copyright (c) Facebook, Inc. and its affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 *
 * @format
 * @flow
*/

'use strict';

const React = require('react');
const {Alert, Button, View} = require('react-native');

class XHRExampleAbortController extends React.Component<{}, {}> {
  _timeout: any;

  _submit(abortDelay) {
    clearTimeout(this._timeout);
    // eslint-disable-next-line no-undef
    const abortController = new AbortController();
    fetch('https://facebook.github.io/react-native/', {
      signal: abortController.signal,
    })
      .then(res => res.text())
      .then(res => Alert.alert(res))
      .catch(err => Alert.alert(err.message));
    this._timeout = setTimeout(() => {
          abortController.abort();
    }, abortDelay);
  }

  componentWillUnmount() {
    clearTimeout(this._timeout);
  }

  render() {
    return (
      <View>
        <Button
          title="Abort before response"
          onPress={() => {
            this._submit(0);
          }}
        />
        <Button
          title="Abort after response"
          onPress={() => {
            this._submit(5000);
          }}
        />
      </View>
    );
  }
}

module.exports = XHRExampleAbortController;
相关问题