Showing posts with label Ternary Operator. Show all posts
Showing posts with label Ternary Operator. Show all posts

3 December 2021

Ternary operator example in react js

import React from "react";

function TernaryOperatorEXP2(props) {
    const status=1
  return (
    <div>
      <div>
        <h1>Ternary operator example in react js</h1>
      </div>
      <button> {status === 1 ? "Active" : "DeActive"} </button>
    </div>
  );
}
export default TernaryOperatorEXP2;

 Github link: https://github.com/adi501/react-js-example

2 April 2020

Ternary Operator Example in react js

  • Create Components folder under src folder
  • Add TernaryOperatorEXP1.js file and add below Code
import React, { useState } from "react";

const TernaryOperatorEXP1 = () => {
const [isLoggedIn, setIsLoggedIn] = useState(true);

return (
<div>
<h3>Ternary Operator Example </h3>
{isLoggedIn ? <div> Welcome User</div> : <div> You need to login</div>}
</div>
);
};

export default TernaryOperatorEXP1;
  • Open App.js file and update code as below\
import React from "react";
import ReactDOM from "react-dom";
import TernaryOperatorEXP1 from "./components/TernaryOperatorEXP1";

function App() {
return (
<div className="App">
<TernaryOperatorEXP1 />
</div>
);
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

Output: