如何从此代码中解析语法错误
在下面运行代码会在控制台中引发语法错误。 它指向传播操作员是一个错误, 请我如何解决?
从“ react”;
导入{usestate}
const [dataInput,setDataInput] = useState({ name: "", age: "", id: "" });
const nameChangeHandler = (event) =>{
setDataInput((previousDataInput)=>{...previousDataInput, name:event.target.value})
}
return (
<div>
<label>Username</label>
<input type="text" onChange={nameChangeHandler} />
<label>Age (Years)</label>
<input type="number" />
</div>
);
};
export default FormInputs;```
This is the error
Failed to compile.
./src/components/Form/FormInputs.js
SyntaxError: C:\Users\User\Desktop\Programs\React\academind\src\components\Form\FormInputs.js: Unexpected token (6:37)
4 | const [dataInput,setDataInput] = useState({ name: "", age: "", id: "" });
5 | const nameChangeHandler = (event) =>{
> 6 | setDataInput((previousDataInput)=>{...previousDataInput, name:event.target.value})
| ^
7 | }
running the code below throws a syntax error in the console.
it points to the spread operator as an error ,
please how do i fix it?
import {useState} from "react";
const [dataInput,setDataInput] = useState({ name: "", age: "", id: "" });
const nameChangeHandler = (event) =>{
setDataInput((previousDataInput)=>{...previousDataInput, name:event.target.value})
}
return (
<div>
<label>Username</label>
<input type="text" onChange={nameChangeHandler} />
<label>Age (Years)</label>
<input type="number" />
</div>
);
};
export default FormInputs;```
This is the error
Failed to compile.
./src/components/Form/FormInputs.js
SyntaxError: C:\Users\User\Desktop\Programs\React\academind\src\components\Form\FormInputs.js: Unexpected token (6:37)
4 | const [dataInput,setDataInput] = useState({ name: "", age: "", id: "" });
5 | const nameChangeHandler = (event) =>{
> 6 | setDataInput((previousDataInput)=>{...previousDataInput, name:event.target.value})
| ^
7 | }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
docs: https://developer.mozilla.org/en-us/docs/web/javascript/javascript/reference/reference/functions/arrow_functions/arrow_functions
doc所说的话,如果您想返回对象文字表达式没有返回键盘您需要将对象包装在括号内:
或者,您可以使用返回 keywoard这样:
Docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions
As the doc says if you want to return an object literal expression without the return keywoard you need to enclose the object inside parentheses:
Alternatively you can use the return keywoard like this:
您必须在setDatainput中返回对象,而不是箭头函数
You must return an object in setDataInput, not an arrow function