[英]"Parsing error: Argument expression expected. eslint" with Typescript map function in React
From this line of code从这行代码
{inputFields.map(if => <Input inputField={if} /> )}
I am getting this error on the word map我在单词地图上收到此错误
Parsing error: Argument expression expected.eslint
inputFields is inside a form, which is inside a div inputFields 位于表单内,该表单位于 div 内
<div>
<form>
{inputFields.map...}
</form>
</div
And input fields is an array输入字段是一个数组
inputFields: InputField[]
This answer suggests that this could have something to do with the typescript version.这个答案表明这可能与打字稿版本有关。
In my package.json
, I have that I'm using "typescript": "^3.8.3"
, but in the bottom right, I think it's saying that I'm using version 4.0.2?在我的
package.json
,我使用的是"typescript": "^3.8.3"
,但在右下角,我认为它是在说我使用的是 4.0.2 版?
When I click on that number, I can change the number to 3.9.7
but that doesn't change the error当我点击那个数字时,我可以将数字更改为
3.9.7
但这不会改变错误
Other answers that I've seen are not relevant to my use case with the map function我看到的其他答案与我使用 map 函数的用例无关
if
is a keyword in Javascript. if
是 Javascript 中的关键字。 Try renaming your variable:尝试重命名您的变量:
{inputFields.map(field => <Input inputField={field} /> )}
Statements vs Expressions语句与表达式
An expression is code that resolves to a value表达式是解析为值的代码
const add = (a, b) => a + b
add(10, 20) // => 30
'Hello' // => 'Hello'
A statement is code that makes something happen语句是使某事发生的代码
if (x < 10) // doesn't resolve
break // doesn't resolve
switch // doesn't resolve
Therefore the warning: "“Parsing error: Argument expression expected. eslint” is telling you that an expression (something that resolves to a value) is expected but instead you have passed a statement (which cannot be resolved to a value)因此,警告:““解析错误:期望的参数表达式。eslint”告诉您需要一个表达式(解析为值的东西),但您已经传递了一个语句(无法解析为值)
You cannot assign a statement to a variable, only expressions.您不能将语句分配给变量,只能分配表达式。
您不能使用带有关键字的变量,例如 if 或 var 作为 js 中的变量,因此您需要重命名变量的名称,因为它应该可以工作
{inputFields.map((data)=> <Input inputField={data} /> )}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.