javascript / beginner
Snippet
Short-Circuiting with Logical AND (&&)
In React, you can use the JavaScript logical AND operator (&&) to conditionally render elements. If the first condition is true, the element after && will be rendered; if false, React ignores it.
snippet.js
javascript
1
2
3
4
5
6
7
8
function Welcome({ isLoggedIn }) {return (<div>{isLoggedIn && <h1>Welcome back!</h1>}<p>Enjoy your stay.</p></div>);}
react
Breakdown
1
{isLoggedIn && <h1>Welcome back!</h1>}
If isLoggedIn is true, the h1 tag is returned. If false, nothing is rendered for this expression.