Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use if statement in React JSX

Can you use a if statement in JSX like this?

    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?

like image 498
morne Avatar asked Sep 11 '17 13:09

morne


People also ask

Can you use if statements in React JSX?

Use the ternary operator to write conditionals in your JSX In React, we must include expressions (something that resolves to a value), not statements within our JSX. This is why we must write conditionals in our JSX only with ternaries and not if-statements.

What is ${} in JSX?

The ${} is template literals.

Can I use ternary operator in JSX?

There's a more compact way to write conditionals in JSX: the ternary operator. The ternary operator works the same way in React as it does in regular JavaScript.


2 Answers

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 to expression, and false && expression always evaluates to 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.

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. If it is false, React won't render anything. Remember, you have to wrap inline JavaScript expressions in JSX with { … }, you can't just have it inside JSX.

Using if/else statements directly is JSX will cause it to be rendered literally as text, which isn't desired. You also can't use them in inline JavaScript expressions because if statements are not expressions, so this won't work:

{
  if(x) y
}
like image 88
Andrew Li Avatar answered Oct 21 '22 20:10

Andrew Li


As per DOC:

if-else statements don't work inside JSX. This is because JSX is just syntactic sugar for function calls and object construction.


We can't use if-else statement or any other statement directly inside JSX, only expressions are allowed.

Expressions inside JSX:

Wou can embed any JavaScript expression in JSX by wrapping it in curly braces.

To put any expression we need to use {}, so instead of if use && operator or ternary operator for conditional rendering.

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>;
like image 30
Mayank Shukla Avatar answered Oct 21 '22 21:10

Mayank Shukla