Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/22.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 ReactJS es lint:Return语句不应包含赋值_Javascript_Reactjs_Typescript_Eslint_Arrow Functions - Fatal编程技术网

Javascript ReactJS es lint:Return语句不应包含赋值

Javascript ReactJS es lint:Return语句不应包含赋值,javascript,reactjs,typescript,eslint,arrow-functions,Javascript,Reactjs,Typescript,Eslint,Arrow Functions,es lint在这种情况下失败:Return语句不应包含赋值no Return assign 我已经研究了这条线索,但没有结果: 我能在这里做些什么来满足es lint的要求 我的状态变量 const[monthlyIncidents,setMonthlyIncidents]=useState([ //月事故[0]->1月…月事故[11]->12月 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ])我猜它将+=1视为赋值(巧合的是,这也是一种状态突变) 您可能只需在当前

es lint在这种情况下失败:Return语句不应包含赋值no Return assign

我已经研究了这条线索,但没有结果:

我能在这里做些什么来满足es lint的要求

我的状态变量
const[monthlyIncidents,setMonthlyIncidents]=useState([
//月事故[0]->1月…月事故[11]->12月
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,

])
我猜它将
+=1
视为赋值(巧合的是,这也是一种状态突变)

您可能只需在当前状态中添加一个即可。这可能会将数组转换为对象,因此请小心

setMonthlyIncidents((prevIncidents) => {
  return {
    ...prevIncidents,
    [incidentMonth]: prevIncidents[incidentMonth] + 1,
  }
})
通常,对于这样的状态更新,最好将现有状态映射到下一个状态并更新元素。看起来,
incidentMonth
只是一个数组索引,因此您可以按索引匹配事件。这保证了状态仍然是一个数组

setMonthlyIncidents((prevIncidents) =>
  prevIncidents.map((incident, index) =>
    incident + index === incidentMonth ? 1 : 0
  )
);
试一试

Setmonthly事件( { …以前的事件, [incidentMonth]:先前事件[incidentMonth]+1

})


您只需要在返回状态时不分配状态。另外,我真的不明白您为什么要创建一个全新的函数来设置状态?(
setStateVariable
hook的作用是什么?)。您还可以为该行禁用es lint。

顺便说一句,在像这样更新之后,每月的事件将是对象而不是数组。我不确定这是否是你真正想要的。你在这一点上是对的,帕维尔。谢谢你的洞察力@非常感谢你,德鲁。两种方法都有效,第一种将数组转换为对象的方法是正确的。此外,我从未想过在这种情况下使用map函数。它更灵活@约瑟菲德太棒了!如果这充分解决了您的问题,并且您觉得这很有帮助,那么请接受回答并投票表决。干杯