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
1 Answer
Reset to default 9Axios 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
版权声明:本文标题:javascript - React abortcancel AJAX request. Axios or XHR - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741399480a2376579.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论