反应 JS 条件更新状态



我有一个更新状态的函数,我只希望它基于以下 indexY 位置工作。

if indexY is === 0 &&
if indexY is === 5 &&
if indexY is === 10 

这是我的功能:

if(this.state.indexY > 0){
if(targetX < 0 || targetX > actionLength) {
return;
} else {
this.setState({
indexX: targetX,
transform,
});
}
}

请注意,如果我使用if(this.state.indexY === 0)这不起作用 - 我不明白为什么?它仅在我使用当前存在的内容时才有效if(this.state.indexY > 0)

这是因为你在表达式中使用了 &&。 indexY 不能同时为 0、5 和 10。用途 ||相反

最新更新