react 异步请求渲染已经销毁的元素的错误的解决

2018-12-04  本文已影响0人  lovinglili

装饰器在react里面的配置

react项目使用的是create-react-app:

 function inject_unount (target) {

    // 改装componentWillUnmount,销毁的时候记录一下
    let next = target.prototype.componentWillUnmount
    target.prototype.componentWillUnmount = function () {
        if (next) next.call(this, ...arguments);//先判断componenteillunmout存在原有的一  些东西不,存在就执行
        this.unmount = true
    }
    // 对setState的改装,setState查看目前是否已经销毁
    let setState = target.prototype.setState //原有的setState
    target.prototype.setState = function () {
        if ( this.unmount ) return ;
        setState.call(this, ...arguments)
    }
 }

@inject_unount
class BaseComponent extends Component {

}
//通过改装这俩函数,当元素销毁的时候,就不再执行setState,不再re-render
上一篇 下一篇

猜你喜欢

热点阅读