如何修复React中的'eslint(no-unused-vars)'错误

时间:2019-08-19 08:46:14

标签: javascript reactjs eslint

由于我已经在项目中安装了babel-eslinteslint-plugin-reacteslint-plugin-es并在.eslintrc中对其进行了配置,所以似乎大多数奇怪的问题在以前没有输出之前消失了。但这仍然是一个令我非常困惑的问题。这是我的React组件之一中的函数:

mouseMove = (e) => {
    window.onmousemove = (e) => {
        // ...
    };
}

'e'被声明但从未使用过(no-unuesd-vars)

2 个答案:

答案 0 :(得分:2)

如果您不使用e变量,则应将其删除:

mouseMove = () => {
   window.onmousemove = () => {
    // ...
   };
}

答案 1 :(得分:0)

上面的e已通过再次使用名为mouseMove的参数在e范围内声明一个函数,从而shadowed了。无法访问e内部的外部onmouseover,因此eslint会抱怨。

您可以通过删除e的参数mouseMove或重命名来解决此问题。

希望这会有所帮助。

相关问题