如何从此代码中解析语法错误

发布于 2025-01-23 10:32:07 字数 1157 浏览 3 评论 0原文

在下面运行代码会在控制台中引发语法错误。 它指向传播操作员是一个错误, 请我如何解决?

从“ 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

习惯成性 2025-01-30 10:32:07

返回对象的文字表达式需要围绕表达的括号:

params =&gt; ({foo:“ a” a})//返回对象{foo:“ a”}

docs: https://developer.mozilla.org/en-us/docs/web/javascript/javascript/reference/reference/functions/arrow_functions/arrow_functions

doc所说的话,如果您想返回对象文字表达式没有返回键盘您需要将对象包装在括号内:

setDataInput((previousDataInput)=>({...previousDataInput, name:event.target.value}))

或者,您可以使用返回 keywoard这样:

setDataInput((previousDataInput)=>{
 return {...previousDataInput, name:event.target.value};
})

To return an object literal expression requires parentheses around expression:

params => ({foo: "a"}) // returning the object {foo: "a"}

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:

setDataInput((previousDataInput)=>({...previousDataInput, name:event.target.value}))

Alternatively you can use the return keywoard like this:

setDataInput((previousDataInput)=>{
 return {...previousDataInput, name:event.target.value};
})
感悟人生的甜 2025-01-30 10:32:07

您必须在setDatainput中返回对象,而不是箭头函数

You must return an object in setDataInput, not an arrow function

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文