报错内容
Can’t perform a React state update 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.
报错代码:
componentDidMount() {
this.initData();
}
initData = () => {
ask('getNoticeUrl', {}).then(res => {
let data = res && res.data ? res.data : [];
let dataSource = data && data.map(item => {
return {
id: item.id,
title: item.title ? item.title : '--',
}
})
this.setState({
dataSource
});
})
}
原因:
在组件销毁之后,异步方法还在执行,因此会报错。我们应该在组件销毁的时候将异步方法撤销。
解决方法:
在组件销毁之后,将异步方法撤销
componentWillUnmount() {
this.setState = (state, callback) => {
return;
};
}