简体   繁体   English

在内联函数中使用时,为什么会出现“ no-unused-var”错误

[英]Why am I getting a 'no-unused-var' error when using in an inline function

Here's a snippent of my code: 这是我的代码的片段:

const prevState = this.state;
this.setState(prevState => ({
  companies_checked: {
    ...prevState.companies_checked,
    [target.value]: target.checked
  }
}));

It is generating the following error: 它生成以下错误:

Line 52:  'prevState' is assigned a value but 
never used  no-unused-vars

I'm using the variable in the inline/arrow function. 我在行内/箭头函数中使用变量。 What's the best way to make the error go away? 使错误消失的最佳方法是什么?

I'm using the variable in the inline/arrow function. 我在行内/箭头函数中使用变量。

No, you don't. 不,你没有。 The prevState that you use in the function is declared as the function's parameter. 您在函数中使用的prevState被声明为函数的参数。 It is shadowing the const -declared prevState from the outer scope. 它从外部作用域中隐藏了const声明的prevState

What's the best way to make the error go away? 使错误消失的最佳方法是什么?

Assuming the function is working as is, just omit the const declaration. 假设函数按原样工作,则省略const声明。

Try this (ie remove the line where you declare prevState ): 尝试以下操作(即删除声明prevState的行):

this.setState(prevState => ({
  companies_checked: {
    ...prevState.companies_checked,
    [target.value]: target.checked
  }
}));

Eslint is complaining that you have declared the constant prevState without ever using it, afterwards. Eslint抱怨说,此后您已经声明了常量prevState却从未使用过。 The correct way to fix the problem is to simply not declare it in the first place. 解决该问题的正确方法是根本不首先声明它。

The prevState inside your setState call is correct - the first argument setState sends to your callback function is the previous state, and this is the one you want to use inside the callback function. prevState你里面setState调用是正确的-第一个参数setState发送到你的回调函数是以前的状态,这就是你想要的回调函数内使用的。

I think it is just naming conflict. 我认为这只是命名冲突。

  // you already declared prevState
    const prevState = this.state;
    // change to this.setState(previousState => 
    this.setState(prevState => ({
      companies_checked: {
        ...prevState.companies_checked,
        [target.value]: target.checked
      }
    }));

You actually did not use prevState that you declared 您实际上没有使用您声明的prevState

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在没有ESLint no-unused-var错误的情况下公开全局javascript函数? - How do you expose a global javascript function without ESLint no-unused-var error? 如何使用谷歌地图解决'no-unused-var'的ESLint错误 - how can i solve ESLint error that 'no-unused-var' with google map Eslint No-unused-var用于装饰工厂 - Eslint no-unused-var for decorator factory 我的 javascript 的问题。 错误:分配了一个值但从未使用过。 [no-unused-var] - Issues with my javascript. Error: is assigned a value but never used. [no-unused-var] 为什么我收到“未使用的默认导出”错误? - Why am I getting "unused default export" error? 为什么在构建浏览器中的Eclipse中使用数字var时我没有得到输出? - why i am not getting output when i am using number var in Eclipse in build browser.? 当我尝试访问$ http var时,为什么会出现此错误? - Why am I getting this error when I try and access the $http var? 在Mocha测试中绕过ESLint的`no-unused-var` for Should - Bypass ESLint's `no-unused-var` for Should in a Mocha test 为什么在 Javascript 中使用“类”时会出现语法错误 - Why am I getting a Syntax error when using “class” in Javascript 为什么在使用 Material UI 时会出现此错误? - Why am I getting this error when using Material UI?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM