How to use if/else Statement in React JS?
Oct 14, 2022 . Admin
Hello Friends,
In this short tutorial we will cover how to use the if/else statement in react js. This article goes in detailed on the if/else statement in react js. This article goes in detailed on if/else statement in react js example. I explained simply about react js if else condition. You just need to do some step to done react js if else condition example.
This example is how to use if/else statement in react js. I will exaplin if/else statement in react js. if/else statement is define return true or false value.
So, let's start following example:
Step 1: Create React JS AppIn this step, open your terminal and execute the following command on your terminal to create a new react app:
npx create-react-app reactjs-my-exampleStep 2: Create index.js file src/index.js
import React, { useState } from "react"; import ReactDOM from "react-dom/client"; function Array(){ const [name,setName] = useState('piyush') if (name == 'piyush') { return <h1>This is Define!</h1>; } else { return <h1>This is Not Define!</h1>; } }; const root = ReactDOM.createRoot(document.getElementById('root')); root.render(<Array />);Run React JS App:
npm start
Output:
I hope it can help you...