Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript ESLint警告(应使用非回调方法)-如何处理?_Javascript_Reactjs_Eslint - Fatal编程技术网

Javascript ESLint警告(应使用非回调方法)-如何处理?

Javascript ESLint警告(应使用非回调方法)-如何处理?,javascript,reactjs,eslint,Javascript,Reactjs,Eslint,如何解决来自ESLint的此警告 我有一个事件侦听器,用于调整componentDidMount内部的大小: componentDidMount() { window.addEventListener('resize', this.resizeEditor); } componentWillUnmount() { window.removeEventListener('resize', this.resizeEditor); } 以及我在调整大小时更新的方法: resizeEdi

如何解决来自ESLint的此警告

我有一个事件侦听器,用于调整componentDidMount内部的大小:

componentDidMount() {
   window.addEventListener('resize', this.resizeEditor);
}

componentWillUnmount() {
   window.removeEventListener('resize', this.resizeEditor);
}
以及我在调整大小时更新的方法:

resizeEditor = () => {
   if (this.editor) this.editor.layout();
}
它是这样工作的,但我得到一个警告:

[eslint]应将方法用于非回调[类首选方法/首选方法]

如果我将箭头功能更改为:

resizeEditor() {
   if (this.editor) this.editor.layout();
}
当然,它找不到这个编辑器。这超出了范围

如果我尝试将编辑器传递给我的调整大小功能:

 window.addEventListener('resize', () => this.resizeEditor(this.editor));

 resizeEditor(editor) {
   if (editor) editor.layout();
 }
我得到另一个警告:

[eslint]预期类方法“resizeEditor”将使用“this”。[类方法使用这个]

所以,我在这里有点被阻止了,我要走什么路才能不得到这些警告


谢谢

这只是我的观点,但你到底应该多关心ESLint呢?只要代码清楚它的功能(和工作原理!),那么问题出在哪里?不要故意杀掉完全可以接受的代码,只是为了确认ESLint开发人员的意见。也许可以将ESLint配置为您喜欢的行为,这样就不会形成留下警告的习惯though@NiettheDarkAbsol我在这里的立场有些反对,但我仍然同意你的看法。底线-不要更改代码。改变规则。这不取决于ESLint开发人员,而是个人偏好。所有规则都是可配置的,可以让它们成为您想要的任何东西-ESLint附带了默认配置,但我认为这条规则甚至不在其中。在我看来,
window.addEventListener('resize',()=>this.resizeEditor(this.editor))有点错误
addEventListener
将元素绑定为上下文。使用arrow函数可以替代此功能。@ItaloBorges sorry I ment
window.addEventListener('resize',this.resizeEditor.bind(this))