- Introduction In this section, we’ll dive into the If-Else in JSX for conditional rendering in React. We’ll explore how to use if-else statements inside JSX to create dynamic UIs. Let’s learn from Geek and Gal’s conversation!
- Using If-Else in JSX with IIFE
function DisplayMessage({ isLoggedIn }) {
return (
<div>
{(() => {
if (isLoggedIn) {
return <h1>Welcome back, user!</h1>;
} else {
return <h1>Please log in.</h1>;
}
})()}
</div>
);
}
- Hilarious Example
function KnockKnockJoke({ isReady }) {
return (
<div>
{(() => {
if (isReady) {
return (
<>
<p>Knock knock.</p>
<p>Who's there?</p>
<p>Alpaca who?</p>
<p>Alpaca the suitcase, you load up the car!</p>
</>
);
} else {
return <p>Let me know when you're ready for a joke!</p>;
}
})()}
</div>
);
}
- Conclusion Using If-Else in JSX with Immediately Invoked Function Expressions (IIFE) is an effective way to achieve conditional rendering in React. It allows you to create dynamic UIs that can adapt to different conditions. Keep learning and experimenting to enhance your React skills! 😄