如果...那么在 JSX 中 Javascript

If...then in JSX for Javascript

我一直在写 Coffeescript/CJSX,这是有效的语法:

<div>
   <input type={if @state.name == "Test" then "checkbox"}/>
</div>

如何对普通 Javascript 执行相同的操作?没有 if...then 子句,我认为我不能像在 Coffee 中那样进行内联 if 检查?

How to write an inline IF statement in JavaScript?

中获取答案

您可以使用语法 10 < 11 ? do this : do that,其中 ?then:else

您还可以使用逻辑运算符:

<input type={(state.name == "Test" && "checkbox") || "text"}>

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators#Short-Circuit_Evaluation