问题描述
在使用React时,出现以下错误,但我不想使用es-lint-disable
-
使用以下代码,我得到
Do not nest ternary expressions.eslintno-nested-ternary
的棉绒错误,该如何解决?getDaySuffix = day => (day === (1 || 21 || 31) ? 'st' : day === (2 || 22) ? 'nd' : day === (3 || 23) ? 'rd' : 'th');
-
在尝试遵循
时遇到此错误Assignment to property of function parameter 'data'.eslintno-param-reassign
如果(重复发生的月份){ 删除data.date; }
-
使用以下代码,我得到一个错误
Assignment to property of function parameter 'carry'.eslintno-param-reassign
(carry,current) => { if (current.field === 'access_group_uuid') { carry[current.field] = (carry[current.field] || []).concat( current.value,); } else { carry[current.field] = carry[current.field] ? [carry[current.field],current.value] : current.value; } return carry; },{},);```
-
使用以下代码,我得到了
Unexpected dangling '_' in '_id'.eslintno-underscore-dangle
的错误。在这里,我从后端获取的数据无法更改。如何解决棉绒错误?const data = { _id: reporData._id,name: reporData.name }; const data = { _id: reporData._id,name: reporData.name,emails,recurrence: obj.recurrence,scheduled_report: true,// eslint-disable-next-line scheduled_report_id: obj._id,};```
有人可以帮我吗?我试图搜索错误,但找到了// eslint-disable-next-line
解决方案,但我不想要该解决方案。可以请任何人帮我吗?
谢谢
解决方法
-
嵌套三元表达式会使代码更难以理解。
https://eslint.org/docs/rules/no-nested-ternary改为定义常规功能。
const getDaySuffix = day => {
if (day === 1 || day === 21 || day === 31) return 'st'
if (day === 2 || day === 22) return 'nd'
if (day === 3 || day === 23) return 'rd'
return 'th'
}
-
此规则旨在防止由于修改或重新分配功能参数而引起的意外行为。 https://eslint.org/docs/rules/no-param-reassign克隆数据对象并使用它。
-
与#2相同。通过更新
carry
,您可以将该对象更改为原始结构。创建新对象,对其进行更新并返回。
(carry,current) => {
const carryClone = {...carry}
if (current.field === 'access_group_uuid') {
carryClone[current.field] = (carryClone[current.field] || []).concat(current.value)
} else {
carryClone[current.field] = carryClone[current.field] ? [carryClone[current.field],current.value] : current.value
}
return carryClone
}
- 此规则禁止在标识符中悬挂下划线。 https://eslint.org/docs/rules/no-underscore-dangle
对象密钥不能为_id
。只能使用id
。
const data = { id: reporData._id,name: reporData.name };