Conditional rendering in React means showing different UI based on conditions — just like using if, ?: (ternary), or && in JavaScript. It lets you display or hide components based on state, props, or any logic in your app.
Example: show a “Login” button when the user is logged out and a “Logout” button when they are logged in.
React doesn’t have a special “conditional rendering” syntax. Instead, it uses normal JavaScript expressions inside JSX:
null: to render nothing when a condition fails.if StatementsUse regular if conditions inside the component body to choose what to return.
// Using if...else to show different greetings
function UserGreeting({ isLoggedIn }) {
if (isLoggedIn) {
return <h4>Welcome back, User!</h4>;
} else {
return <h4>Please log in.</h4>;
}
}
// Usage:
<UserGreeting isLoggedIn={true} />
For simple either/or UI, the ternary operator keeps JSX compact and readable.
// Using a ternary to choose between morning and evening text
function Greeting({ isMorning }) {
return (
<h4>
{isMorning ? "Good Morning ☀️" : "Good Evening ?"}
</h4>
);
}
&&)Render something only when a condition is true. If it’s false, React ignores the element.
// Only show the message if hasMessage is true
function Notification({ hasMessage }) {
return (
<div>
{hasMessage && <p>You have a new message!</p>}
</div>
);
}
For multiple branches in a small area, an immediately invoked function expression (IIFE) can keep logic close to the JSX.
// Use an inline function to handle multiple status states
function StatusMessage({ status }) {
return (
<div>
{(() => {
if (status === "loading") return <p>Loading...⏳</p>;
if (status === "error") return <p>❌ Something went wrong!</p>;
return <p>✅ Data loaded successfully!</p>;
})()}
</div>
);
}
Often you conditionally render entire components instead of small elements.
// Swap between Admin and User dashboards based on isAdmin
function AdminPanel() {
return <h5>Admin Dashboard</h5>;
}
function UserPanel() {
return <h5>User Dashboard</h5>;
}
function Dashboard({ isAdmin }) {
return (
<div>
{isAdmin ? <AdminPanel /> : <UserPanel />}
</div>
);
}
For more than two options, you can chain ternaries — but keep an eye on readability.
// Choose a different message based on the temperature
function Weather({ temp }) {
return (
<p>
{temp > 30
? "It's hot ?"
: temp > 20
? "Nice and warm ?️"
: "Cold day ❄️"}
</p>
);
}
<UserGreeting isLoggedIn={true} /> → “Welcome back, User!”<Greeting isMorning={false} /> → “Good Evening ?”<Notification hasMessage={false} /> → No message line rendered at all<Weather temp={25} /> → “Nice and warm ?️”Notice that when a condition is false (like hasMessage), React simply renders nothing for that expression — it doesn’t leave a blank hole on the screen.
null from a component to render nothing when a section should be hidden.isLoggedIn is true.&& to display a message when a list is empty.Goal: Learn how to render UI elements dynamically using different conditional rendering patterns for clean and responsive React components.