无法在未安装的组件上调用 setState(或 forceUpdate)
Can't call setState (or forceUpdate) on an unmounted component
我试图在组件更新后从服务器获取数据,但我无法做到这一点。据我了解 componentWillUnmount
是在组件即将被销毁时调用的,但我从不需要销毁它所以它对我没用。什么是解决方案?我应该什么时候设置状态?
async componentDidUpdate(prevProps, prevState) {
if (this.props.subject.length && prevProps.subject !== this.props.subject) {
let result = await this.getGrades({
student: this.props.id,
subject: this.props.subject
});
this.setState({
subject: this.props.subject,
grades: result
});
}
}
async getGrades(params) {
let response, body;
if (params['subject'].length) {
response = await fetch(apiRequestString.gradesBySubject(params));
body = await response.json();
} else {
response = await fetch(apiRequestString.grades(params));
body = await response.json();
}
if (response.status !== 200) throw Error(body.message);
return body;
}
完整错误:
Warning: Can't call setState (or forceUpdate) on an unmounted component. This is a no-op,
but it indicates a memory leak in your application. To fix, cancel all subscriptions and
asynchronous tasks in the componentWillUnmount method.
我在这个例子中使用的一个常见模式是
componentWillUnmount() {
this.isCancelled = true;
}
然后在等待异步函数解析的代码中,您将在设置状态之前添加检查:
async componentDidUpdate(prevProps, prevState) {
if (this.props.subject.length && prevProps.subject !== this.props.subject) {
let result = await this.getGrades({
student: this.props.id,
subject: this.props.subject
});
!this.isCancelled && this.setState({
subject: this.props.subject,
grades: result
});
}
}
这将停止 unmounted/unmounting 组件上的任何状态设置
接受的答案有效,并且是在组件渲染方法(getInitialState、componentWillMount、componentDidMount)中调用异步函数问题的有效解决方法。
但更好的做法是使用 Redux 和 Flux 等状态管理助手和全局存储,这可能会避免多个 setStates 的问题。
我试图在组件更新后从服务器获取数据,但我无法做到这一点。据我了解 componentWillUnmount
是在组件即将被销毁时调用的,但我从不需要销毁它所以它对我没用。什么是解决方案?我应该什么时候设置状态?
async componentDidUpdate(prevProps, prevState) {
if (this.props.subject.length && prevProps.subject !== this.props.subject) {
let result = await this.getGrades({
student: this.props.id,
subject: this.props.subject
});
this.setState({
subject: this.props.subject,
grades: result
});
}
}
async getGrades(params) {
let response, body;
if (params['subject'].length) {
response = await fetch(apiRequestString.gradesBySubject(params));
body = await response.json();
} else {
response = await fetch(apiRequestString.grades(params));
body = await response.json();
}
if (response.status !== 200) throw Error(body.message);
return body;
}
完整错误:
Warning: Can't call setState (or forceUpdate) on an unmounted component. This is a no-op,
but it indicates a memory leak in your application. To fix, cancel all subscriptions and
asynchronous tasks in the componentWillUnmount method.
我在这个例子中使用的一个常见模式是
componentWillUnmount() {
this.isCancelled = true;
}
然后在等待异步函数解析的代码中,您将在设置状态之前添加检查:
async componentDidUpdate(prevProps, prevState) {
if (this.props.subject.length && prevProps.subject !== this.props.subject) {
let result = await this.getGrades({
student: this.props.id,
subject: this.props.subject
});
!this.isCancelled && this.setState({
subject: this.props.subject,
grades: result
});
}
}
这将停止 unmounted/unmounting 组件上的任何状态设置
接受的答案有效,并且是在组件渲染方法(getInitialState、componentWillMount、componentDidMount)中调用异步函数问题的有效解决方法。
但更好的做法是使用 Redux 和 Flux 等状态管理助手和全局存储,这可能会避免多个 setStates 的问题。