[英]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.