How to Use Ternary Operator in React JS?

By Hardik Savani September 6, 2020 Category : React JS

Hello Dev,

Hello all! In this article, we will talk about react ternary conditional rendering. I’m going to show you about react ternary operator example. This article goes in detailed on how to write a ternary operator in react. i would like to show you how to use ternary operator in react js.

In this post, i will give you one simple example of how to write ternary conditional statement in react native app. you can simply use ternary in render component. so let's see bellow example that will help you to understand how it works.

First example will cover react ternary statement in render function.component.

So, let's see this example:

Example 1:

src/App.js

import React from 'react';

import logo from './logo.svg';

import './App.css';

function App() {

const isLoading = 1;

return (

<div className="container">

<h1>React If Condition Example - ItSolutionStuff.com</h1>

<div>

{isLoading == 1 ?

<p>Value is One.</p> :

<p>Value is Another.</p>}

</div>

</div>

);

}

export default App;

Output:

Value is One

I hope it can help you...

Tags :
Shares