If statement 将if语句转换为三值

If statement 将if语句转换为三值,if-statement,ternary,If Statement,Ternary,嗨 我需要把这个if语句转换成三元。我对三分之一不太在行。。但我需要把这个按在按钮上。我必须添加更多的“ifs”,但是如果我能把它转换成正确的语法,我应该能够将其余的都转换成它 if (this.state.selectedMeat === 'Ham'){ this.setState(this.state.calories = this.state.calories + this.state.meats[0].calories) } console.log('did calories

我需要把这个if语句转换成三元。我对三分之一不太在行。。但我需要把这个按在按钮上。我必须添加更多的“ifs”,但是如果我能把它转换成正确的语法,我应该能够将其余的都转换成它

if (this.state.selectedMeat === 'Ham'){
  this.setState(this.state.calories = this.state.calories + this.state.meats[0].calories)
  }
  console.log('did calories go up?')
}
所以我希望上面的if语句在一个按钮onclick三元内工作

<button onClick={ 'the stuff goes here '} >Add Me Up!</button>
把我加起来!

谢谢你的帮助

该代码与三元代码并不完全兼容。它没有其他情况,只是产生了副作用。如果这些都违背了三元函数的全部目的。只需将其包装在函数中,以便在单击
onClick
时放入一些内容。尝试将其作为一个函数开始,但之后我无法使该函数具有访问状态的权限。我尝试在构造函数中编写函数,该函数似乎在加载页面时立即运行,而不是在onclick期间运行。所以我想我可以把它做成三元,然后把它放在按钮上。我还有很多if语句要放进去。