用类修饰器解决React内存泄露问题,怎样修改才能避免ESLint报错?

为了解决React内存泄露的问题,从网上找到了方法解决,但是相关代码在ESlint中报错比较多,自己尝试着解决这些报错,但是一直没能成功。
求助一下各位大佬。
图片描述

类修饰器的代码

export function injectUnount (target){
  const next = target.prototype.componentWillUnmount
  target.prototype.componentWillUnmount = function () {
      if (next) next.call(this, ...arguments);
      this.unmount = true
   }
  let setState = target.prototype.setState
  target.prototype.setState = function () {
      if ( this.unmount ) return ;
      setState.call(this, ...arguments)
  }
}
阅读 1.9k
2 个回答

这样的看到你代码报错的eslint 的规则

  • 参数再次赋值 (168, 173行)
  • 推荐使用es6的不定量参数 (169, 175行)
  • 推荐使用析构(172)
  • 推荐使用 const (172)
  • 推荐使用命名参数,也就是不要使用匿名函数(173, 168)

修改如下:

export function injectUnount (target){
  const next = target.prototype.componentWillUnmount
  const cmp = target;
  cmp.prototype.componentWillUnmount = function componentWillUnmount(...args) {
      if (next) next.call(this, ...args);
      this.unmount = true
   }
  
  const { setState: originalSetState } = target.prototype;

  cmp.prototype.setState = function setState(...args) {
      if ( this.unmount ) return ;
      originalSetState.call(this, ...args)
  }
}
可以试试,可能会出问题,但是我想应该解决大部分报错了
/* eslint-disable */

放到顶上

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题