react componentWillUnmount 清除定时器 ajax请求 离开页面后还是会报内存泄漏的错误
跳转页面后
在componentWillUnmount中,this.setState = ()=>false; 停止了异步 也清除了计时器 还是会报错
报错内容
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.
无法在未挂载的组件上执行React状态更新。这是一个无操作,但它表明应用程序中存在内存泄漏。要修复这个问题,请取消componentWillUnmount方法中的所有订阅和异步任务。
求助
export default class CommonTimer extends Component {
state = {
content: '',
txts: '',
passCurrentTime: 0,
newCurrentTime: 0,
flag: 1
}
countdowm = (timestamp) => {
this.timer = setInterval(() => {
this.setState({ passCurrentTime: this.state.passCurrentTime + 1000 })
let DayTime = timestamp - this.state.passCurrentTime;
console.log('1')
if (DayTime > 1000) {
var Hours = 60 * 60 * 1000,
Minutes = 60 * 1000,
Seconds = 1000;
var hour = addZero(Math.floor(DayTime / Hours)),
minute = addZero(Math.floor((DayTime % Hours) / Minutes)),
second = addZero(Math.floor(((DayTime % Hours) % Minutes) / Seconds));
this.setState({
txts: '距活动结束:',
content: hour + '小时' + minute + '分' + second + '秒'})
}
function addZero(num) {
return num < 10 ? "0" + num : "" + num;
}
}, 1000);
}
aaa = ()=>{
this.props.history.push('buyercenter/coupon')
}
componentDidMount() {
this.setState({ passCurrentTime: this.props.currentTime })
if (this.props.currentTime < this.props.startTime) {
this.countdowm(this.props.startTime);
} else {
this.setState({
txts: '距活动结束:',
content: '00小时00分00秒'
})
}
}
componentWillUnmount(){
clearInterval(this.timer);
this.setState = ()=>false;
}
render() {
return (
<div className={s.goods_item_time}>
<span onClick={this.aaa}> 123123 </span>
<span>{this.state.txts}</span>
<span>{this.state.content}</span>
</div>
)
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论