react 报错0.chunk.js:449884 Warning: Can‘t perform a React state update on an unmounted component. Thi_CSDN_Alisa的博客-程序员宅基地

技术标签: react项目报错  React  

0.chunk.js:449884 Warning: 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.

原因:settimeout未运行完,对setstate的属性值没有起作用导致的。

componentDidMount() {
        
        let _this =this;
        let time = 10;
        let timeFun=setInterval(()=>{
            _this.setState({
                time:time--
            })
            // console.log("倒计时",this.state.time);
            if(this.state.time<=0){
                // console.log("清除定时器")
                _this.setState({
                    conStudyShow:false,
                })
                clearInterval(timeFun)
            }
        },1000);
        console.log("结束");
    }

解决方法:

componentWillUnmount() {
        this.setState = ()=>false;
    }

 

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/Fu_Monkey/article/details/107406463

智能推荐

随便推点