[英]Use if statement in React JSX
Can you use a if statement in JSX like this? 你能在JSX中使用if语句吗?
var chartGraphContent =
<div className={"chartContent"}>
if(this.state.modalityGraph['nca'] > 0){
<div className={"chart-container"}>
<Chart
chartType="ColumnChart"
data = { this.state.modalityGraph?this.state.modalityGraph.chartData['units']:emptyDataRows }
options={chartOptions}
graph_id="modalitiesChart"
width="100%"
height="250px"
/>
</div>
}
</div>;
Something like above? 像上面的东西? Is it possible to have JSX based on a condition?
是否可以根据条件使用JSX?
Use conditional rendering , and since you have no else-case, you can use &&
instead of a ternary operator for brevity: 使用条件渲染 ,并且由于没有其他情况,您可以使用
&&
代替三元运算符以简化:
It works because in JavaScript,
true && expression
always evaluates toexpression
, andfalse && expression
always evaluates tofalse
.它的工作原理是因为在JavaScript中,
true && expression
总是求值为expression
,而false && expression
总是求值为false
。Therefore, if the condition is true, the element right after && will appear in the output.
因此,如果条件为真,则&&之后的元素将出现在输出中。 If it is false, React will ignore and skip it.
如果是假,React将忽略并跳过它。
Thus: 从而:
<div className={"chartContent"}>
{this.state.modalityGraph['nca'] > 0 &&
<div className={"chart-container"}>
<Chart
chartType="ColumnChart"
data = { this.state.modalityGraph?this.state.modalityGraph.chartData['units']:emptyDataRows }
options={chartOptions}
graph_id="modalitiesChart"
width="100%"
height="250px"
/>
</div>
}
</div>
This will only render JSX if a condition is true. 这只会在条件为真时呈现JSX。 If it is false, React won't render anything.
如果为false,则React不会呈现任何内容。 Remember, you have to wrap inline JavaScript expressions in JSX with
{ … }
, you can't just have it inside JSX. 请记住,您必须使用
{ … }
在JSX中包装内联JavaScript表达式,您不能只在JSX中包含它。
Using if/else statements directly is JSX will cause it to be rendered literally as text, which isn't desired. 直接使用if / else语句是JSX将使它按字面呈现为文本,这是不希望的。 You also can't use them in inline JavaScript expressions because if statements are not expressions , so this won't work :
您也不能在内联JavaScript表达式中使用它们,因为if 语句不是表达式 ,所以这不起作用 :
{
if(x) y
}
if-else statements don't work inside JSX.
if-else语句在JSX中不起作用。 This is because JSX is just syntactic sugar for function calls and object construction.
这是因为JSX只是函数调用和对象构造的语法糖。
We can't use if-else
statement or any other statement directly inside JSX, only expressions are allowed. 我们不能在JSX中直接使用
if-else
语句或任何其他语句,只允许表达式。
Expressions inside JSX : JSX中的表达式 :
Wou can embed any JavaScript expression in JSX by wrapping it in curly braces.
您可以通过将其包装在花括号中来在JSX中嵌入任何JavaScript表达式。
To put any expression we need to use {}
, so instead of if
use &&
operator or ternary operator
for conditional rendering. 为了把我们需要使用任何表达
{}
所以不是if
使用&&
运算符或ternary operator
的条件呈现。
By using ternary operator : 通过使用三元运算符 :
var chartGraphContent =
<div className={"chartContent"}>
{
this.state.modalityGraph['nca'] > 0 ?
<div className={"chart-container"}>
<Chart
chartType="ColumnChart"
data = { this.state.modalityGraph?this.state.modalityGraph.chartData['units']:emptyDataRows }
options={chartOptions}
graph_id="modalitiesChart"
width="100%"
height="250px"
/>
</div>
:null
}
</div>;
By using && operator : 通过使用&&运算符 :
var chartGraphContent =
<div className={"chartContent"}>
{
this.state.modalityGraph['nca'] > 0 &&
<div className={"chart-container"}>
<Chart
chartType="ColumnChart"
data = { this.state.modalityGraph?this.state.modalityGraph.chartData['units']:emptyDataRows }
options={chartOptions}
graph_id="modalitiesChart"
width="100%"
height="250px"
/>
</div>
}
</div>;
Better to use with ternary Operator , By doing so you can also add
else
block to your code.最好与三元运算符一起使用,通过这样做,您还可以在代码中添加
else
块。
Try this: 试试这个:
var chartGraphContent =
<div className={"chartContent"}>
{this.state.modalityGraph['nca'] > 0 ?
<div className={"chart-container"}>
<Chart
chartType="ColumnChart"
data = { this.state.modalityGraph?this.state.modalityGraph.chartData['units']:emptyDataRows }
options={chartOptions}
graph_id="modalitiesChart"
width="100%"
height="250px"
/>
</div>
: "<span>Else Block</span>"
}
</div>;
and in case for more complex and large condition you can call inline functions too to return your template , in this way you can avoid your code to become messy. 如果对于更复杂和更大的条件,您也可以调用内联函数来返回模板,这样可以避免代码变得混乱。 here is an example.
这是一个例子。
var ifBlockCode = function ifBlockCode(){
return (
<div className={"chart-container"}>
<Chart
chartType="ColumnChart"
data = { this.state.modalityGraph?this.state.modalityGraph.chartData['units']:emptyDataRows }
options={chartOptions}
graph_id="modalitiesChart"
width="100%"
height="250px"
/>
</div>
)
}
var elseBlockCode = function elseBlockCode(){
return (
<span>Else Block</span>
)
}
var chartGraphContent =
<div className={"chartContent"}>
{this.state.modalityGraph['nca'] > 0 ?
{this.ifBlockCode} : {this.elseBlockCode}
}
</div>;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.