zoukankan      html  css  js  c++  java
  • React防止内存泄漏

    在React开发中,我们可能会遇到警告:

    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”
    

    意思为:我们不能在组件销毁后设置state,防止出现内存泄漏的情况。

    //组件B
        class TestContainer extends Component{
            constructor(){
                super()
                this.state = {
                    isShow:true
                }
            }
            render(){
                return (
                    <div>
                        <button onClick={()=>this.setState({isShow:!this.state.isShow})}>toggle</button>
                        {!!this.state.isShow&&<Test />}
                    </div>
                )
            }
        }
        //组件A
        class Test extends Component{
            constructor(){
                super()
                this.state = {
                    num:0
                }
            }
            getNum=()=>{
                //模拟异步请求
                this.timer = setTimeout(()=>{
                    this.setState({ num: Math.random() })
                },3000)
            }
            render(){
                return (
                    <div onClick={this.getNum} style = {{  100, height: 100, background: 'red' }}>
                        {this.state.num}
                    </div>
                )
            }
        }
      // 在本例子中:
            // 当我们点击组件A时,会发送一个异步请求,请求成功后会更新num的值。
            // 当我们点击组件B时,会控制组件的A的卸载与装载
    

    当我们点击组件A后,组件A需要3秒的时间才能获取到数据并重新更新num的值,假如我们在这3秒内点击一次组件B,
    表示卸载组件A,但是组件A的数据请求依然还在,当请求成功后,组件A已经不存在,此时就会报这个警告(大概意思就是:你组件都没了,你还设置个啥) 

    解决办法:

    本问题出现的原因就是:我们应该在组件销毁的时候将异步请求撤销

    • 在componentWillUnmount中撤销异步请求
    1. axios上有撤销异步请求的方法,但是我们有这么多组件,每次都要撤销岂不是太麻烦了
    2. 我们可以利用一个‘开关的思想’,在组件销毁的时候给this上挂载一个属性,每次发送请求的时候,我们判断一下这个属性是否存在(还是麻烦,每次都要判断)
    3. 基于思路2,我们不想每次判断,因此是不是应该将其封装,利用修饰器对componentWillUnmount和setState进行改装
     function inject_unount (target){
            // 改装componentWillUnmount,销毁的时候记录一下
            let next = target.prototype.componentWillUnmount
            target.prototype.componentWillUnmount = function () {
                if (next) next.call(this, ...arguments);
                this.unmount = true
             }
             // 对setState的改装,setState查看目前是否已经销毁
            let setState = target.prototype.setState
            target.prototype.setState = function () {
                if ( this.unmount ) return ;
                setState.call(this, ...arguments)
            }
        }
        @inject_unount
        class BaseComponent extends Component {
        
        }
        //以后我们写组件时直接继承BaseComponent
    

     转载于:https://segmentfault.com/a/1190000017186299

  • 相关阅读:
    java中static的用法
    java策略设计模式
    java模板设计模式
    Spring 学习笔记 8. 尚硅谷_佟刚_Spring_使用外部属性文件
    Spring 学习笔记 7. 尚硅谷_佟刚_Spring_Bean 的作用域
    Spring学习笔记 6. 尚硅谷_佟刚_Spring_Bean 之间的关系
    Spring学习笔记 5. 尚硅谷_佟刚_Spring_自动装配
    Spring 学习笔记 4. 尚硅谷_佟刚_Spring_属性配置细节
    Spring 学习笔记 3. 尚硅谷_佟刚_Spring_配置 Bean
    Spring 学习笔记 2. 尚硅谷_佟刚_Spring_IOC&DI概述
  • 原文地址:https://www.cnblogs.com/Ewarm/p/14292087.html
Copyright © 2011-2022 走看看