admin管理员组

文章数量:1289483

I just develop a SPA and need help with the asynchronous requests I send by react ponentDidMount to my server side (node). The queries work, but if I change quickly to the different pages, the setState functions within the ajax response are still running. Although the ponents are unmounted.

Can only update a mounted or mounting ponent. This usually means you called setState() on an unmounted ponent. This is a no-op. Please check the code for the undefined ponent.

To avoid this error, I would like to abort/cancel the request which I send on ponentWillUnmount if this is the correct way to do this.

I tried this with xml and with axios but it doesnt work. I hope you have a working example for me. Iam open for other request methods like fetch or something.

The code looks something like this (Axios), but it doesn't cancel the request:

import * as React from 'react';
import axios from 'axios';

let CancelToken = axios.CancelToken;
let source = CancelToken.source();

class MakeAjaxRequest extends React.Component {
  constructor() {
    super();
    this.state = {
      data: {}
    }
  }

  ponentDidMount() {
    const that = this;

    axios.get('/user/12345', {
      cancelToken: source.token
    })
      .then(function (response) {
        if (axios.isCancel(response)) {
          console.log('Request canceled', response);
        } else {
          that.setState({
            data: response.data
          });
        }
      })
      .catch(function (thrown) {
        if (axios.isCancel(thrown)) {
          console.log('Request canceled', thrown.message);
        } else {
          // handle error
        }
      });
  }

  ponentWillUnmount() {
    source.cancel('Operation canceled by the user.');
  }

  //....other stuff, like render the data
}

I just develop a SPA and need help with the asynchronous requests I send by react ponentDidMount to my server side (node). The queries work, but if I change quickly to the different pages, the setState functions within the ajax response are still running. Although the ponents are unmounted.

Can only update a mounted or mounting ponent. This usually means you called setState() on an unmounted ponent. This is a no-op. Please check the code for the undefined ponent.

To avoid this error, I would like to abort/cancel the request which I send on ponentWillUnmount if this is the correct way to do this.

I tried this with xml and with axios but it doesnt work. I hope you have a working example for me. Iam open for other request methods like fetch or something.

The code looks something like this (Axios), but it doesn't cancel the request:

import * as React from 'react';
import axios from 'axios';

let CancelToken = axios.CancelToken;
let source = CancelToken.source();

class MakeAjaxRequest extends React.Component {
  constructor() {
    super();
    this.state = {
      data: {}
    }
  }

  ponentDidMount() {
    const that = this;

    axios.get('/user/12345', {
      cancelToken: source.token
    })
      .then(function (response) {
        if (axios.isCancel(response)) {
          console.log('Request canceled', response);
        } else {
          that.setState({
            data: response.data
          });
        }
      })
      .catch(function (thrown) {
        if (axios.isCancel(thrown)) {
          console.log('Request canceled', thrown.message);
        } else {
          // handle error
        }
      });
  }

  ponentWillUnmount() {
    source.cancel('Operation canceled by the user.');
  }

  //....other stuff, like render the data
}
Share Improve this question asked Oct 26, 2017 at 8:57 Daniel SchinkDaniel Schink 811 silver badge2 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

Axios has the option of cancelling requests:

https://github./axios/axios#cancellation

Another way of preventing setState from firing on some kind of callback after the ponent is unmounted is to have a local boolean variable storing whether or not the ponent is mounted, i.e.

ponentDidMount(){
   this._mounted = true;
}

ponentWillUnmount() {
   this._mounted = false;
}

Then in the axios callback check whether _mounted is true before trying to call setState()

本文标签: javascriptReact abortcancel AJAX request Axios or XHRStack Overflow