2 April 2020

Short Circuit Operator example in react js

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

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

return (
<div>
<h3>Short Circuit Operator Example </h3>
{isLoggedIn && <div> Welcome User</div>}
</div>
);
};

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

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

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

Output:

No comments:

Post a Comment