How to conditionally render an element or text in React?
Learn how to display elements conditionally using if statements, ternary operators, and logical AND in React.
beginnerConditional renderingreactconditional renderingjsx
Published: 10/26/2025
Updated: 10/26/2025
Question
How to conditionally render an element or text in React?
Answer
Conditional rendering in React works the same way as JS — you just decide what to render inside JSX.
Using if statements:
if (isLoggedIn) {
return <Dashboard />;
} else {
return <Login />;
}
Using ternary operators:
<div>{isDark ? 'Dark Mode' : 'Light Mode'}</div>
Using logical AND:
{
notifications.length > 0 && (
<p>You have {notifications.length} new notifications!</p>
);
}
Real-World Example
- Show/hide buttons or loaders
- Toggle UI elements based on authentication or user actions
Quick Practice
Build a component that shows “Welcome!” if logged in, and “Please log in” otherwise.
Summary
Use standard JS conditions (if, ? :, &&) inside JSX for conditional UI logic.
Related Videos
Watch these videos to learn more about this topic
Frequently Asked Questions
What’s the best way to conditionally render?
Use ternary or logical && for simple cases; use functions for complex conditions.
Can I return null to hide components?
Yes. Returning null means React renders nothing.
Stay Updated
Get the latest frontend challenges, interview questions and tutorials delivered to your inbox.