How can I implement a nested if-else statement in ReactJS JSX

0 votes
Can you tell me How can I implement a nested if-else statement in ReactJS JSX?
Mar 12 in Node-js by Ashutosh
• 23,230 points
43 views

1 answer to this question.

0 votes

You can use ternary operators or by defining conditions before returning JSX.

Example: Using Ternary Operator

function Status({ age }) {

    return (

        <div>

            {age >= 18 ? (

                age >= 60 ? <p>Senior Citizen</p> : <p>Adult</p>

            ) : (

                <p>Minor</p>

            )}

        </div>

    );

}

Example: Using a Separate Function

function getStatus(age) {

    if (age >= 18) {

        return age >= 60 ? "Senior Citizen" : "Adult";

    } else {

        return "Minor";

    }

}

function Status({ age }) {

    return <p>{getStatus(age)}</p>;

}

answered Mar 12 by Sahil

Related Questions In Node-js

0 votes
1 answer

How can I check if an array contains a particular string in TypeScript?

You can use includes() method, which checks ...READ MORE

answered Feb 10 in Node-js by Navya
87 views
0 votes
1 answer

How can I modify results in a read-only ng-app?

Modifying data within an AngularJS application that ...READ MORE

answered Feb 10 in Node-js by Navya
103 views
0 votes
1 answer

How to use async functions effectively in React components?

To use async functions effectively in React ...READ MORE

answered Mar 12 in Node-js by Sahil
60 views
0 votes
1 answer
0 votes
1 answer

How does Babel differ from JSX?

Feature Babel JSX Definition A JavaScript compiler that transforms ES6+ code ...READ MORE

answered Mar 12 in Java-Script by Sahil
95 views
0 votes
1 answer

How can I install the Babel plugin for JSX syntax?

Use the following commands: npm install @babel/preset-react --save-dev Steps ...READ MORE

answered Mar 12 in Node-js by Sahil
44 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP