简体   繁体   English

期望表达。 与埃斯林特和反应

[英]Expression expected. with eslint and react

here is my code with a problem 这是我的代码有问题

handleXPosition(e) {
    const x = parseFloat(e.target.value);
    this.setState((prevState) => ({ position: ...prevState.position, x}));
}

this my state: 这是我的状态:

this.state = {
    position: { x: 0.5, y: 0.5 },
    rotate: 0,
    scale: 1,
    width: 250,
    height: 160,
    imageSource: '',
    editMode: false,
 };

my button in my jsx part: 我的jsx部分中的按钮:

<div>
    <TextField
        label="Position X"
        type="range"
        onChange={this.handleXPosition}
        inputProps={{ min: 0, max: 1, step: 0.01 }}
    />
</div>

i have this message dans VS code 我有此消息dans VS代码

[ts] Expression expected. [1109]

in the ...prevState.position part. ...prevState.position部分中。

and I do not understand 我不明白

thank 谢谢

How about the following: 怎么样:

this.setState((prevState) => ({ position: prevState.position, x }));

or even: 甚至:

this.setState((prevState) => ({ ...prevState, x }));

Please change line to following: 请更改以下行:

this.setState((prevState) => ({ position: {...prevState.position}, x}));

or 要么

this.setState((prevState) => ({...prevState, x}));

If you only want to take out the position from your state, you do not need to deconstruct it : 如果您只想从您的状态中取出该position ,则无需对其进行解构:

handleXPosition(e) {
    const x = parseFloat(e.target.value);
    this.setState(prevState => ({ position: prevState.position, x }));
}

And if you want to take out every values from your previous props : 如果您想从以前的道具中取出所有价值,请执行以下操作:

handleXPosition(e) {
    const x = parseFloat(e.target.value);
    this.setState(prevState => ({ ...prevState, x }));
}

EDIT 编辑

To modify the nested x value, you will have to deconstruct your old object into position and only override the x value : 要修改嵌套的x值,您将必须将旧对象解构到适当位置,并且仅覆盖x值:

this.setState(prevState => ({ position: {...prevState.position, x}}));

try it like this: 尝试这样:

EDIT: 编辑:

handleXPosition = (
    e: {
      target: { value },
    },
  ) => {
    this.setState(prevState => ({ position: prevState.position, x: parseFloat(value) }));
  };

edited the answer. 编辑了答案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “解析错误:预期的参数表达式。eslint”与 React 中的 Typescript 映射函数 - "Parsing error: Argument expression expected. eslint" with Typescript map function in React React应用未按预期对更改做出反应。 为什么? - React app is not reacting to changes as expected. Why? 反应 typescript '=' 预期。 TS1005 - React typescript '=' expected. TS1005 如何修复“预期的表达。 ts(1109)”在 VSCode 中? - How do I fix “Expression expected. ts(1109)” in VSCode? IONIC / REACT 第 9:5 行:期望一个赋值或函数调用,而是看到一个表达式 @typescript-eslint/no-unused-expressions - IONIC / REACT Line 9:5: Expected an assignment or function call and instead saw an expression @typescript-eslint/no-unused-expressions 期望分配或 function 调用,而是看到一个表达式 @typescript-eslint/no-unused-expressions (React) - Expected an assignment or function call and instead saw an expression @typescript-eslint/no-unused-expressions (React) eslint JSX返回错误“表达式预期” - eslint JSX return error “expression expected” 我的 React 应用程序没有按预期呈现。 为什么会这样? - My react app doesn't render as expected. Why is this happening? 反应:错误:呈现的钩子比预期的少。 这可能是由 - REACT : Error: Rendered fewer hooks than expected. This may be caused by an 停止 function 未按预期工作。 需要帮忙。 反应 - Stop function is not working as expected. Need Help. React
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM