Components and Props · Lesson 6 of 31
Conditional Rendering in React
Real interfaces change with data: a "Log in" button for guests and an avatar for members, a spinner while loading, an empty state when a list has no items. In React you choose what to render with ordinary JavaScript – if, the ternary operator and &&.
In this lesson
ifand early returns- The ternary operator inside JSX
&&and the "0" pitfall- Loading, error and empty states
if and early return
function Greeting({ user }) {
if (!user) {
return <p>Welcome, guest. <a href="/login">Log in</a></p>; // early return
}
return <p>Welcome back, {user.name}!</p>;
}
function AdminLink({ role }) {
if (role !== "admin") return null; // render nothing
return <a href="/admin">Admin panel</a>;
}
export default function App() {
return (
<>
<Greeting user={null} />
<Greeting user={{ name: "Asha" }} />
<AdminLink role="student" />
<AdminLink role="admin" />
</>
);
}
Welcome, guest. Log in Welcome back, Asha! Admin panel
Returning null tells React to render nothing for that component. Early returns keep the main JSX flat and readable.
The ternary operator
Inside JSX you can't write if, but you can use condition ? a : b:
import { useState } from "react";
export default function App() {
const [isEnrolled, setIsEnrolled] = useState(false);
return (
<div>
<h2>React.js course</h2>
<p>Status: {isEnrolled ? "Enrolled ✅" : "Not enrolled"}</p>
{isEnrolled ? (
<button onClick={() => setIsEnrolled(false)}>Leave course</button>
) : (
<button onClick={() => setIsEnrolled(true)}>Enroll for free</button>
)}
</div>
);
}
React.js course Status: Not enrolled [Enroll for free] › click “Enroll for free” React.js course Status: Enrolled ✅ [Leave course] › click “Leave course” React.js course Status: Not enrolled [Enroll for free]
&& for "show or nothing"
export default function App() {
const unread = 3;
const isPremium = false;
return (
<div>
<h2>Inbox</h2>
{unread > 0 && <p>You have {unread} new messages</p>}
{isPremium && <p>Premium badge</p>}
</div>
);
}
Inbox You have 3 new messages
a && b returns b when a is truthy, otherwise a. React renders nothing for false, null and undefined… but it does render numbers.
The "0" pitfall
export default function App() {
const cart = [];
return (
<div>
<h2>Cart</h2>
{cart.length && <p>{cart.length} items</p>}
</div>
);
}
Cart 0
A stray 0 appears, because 0 && … evaluates to 0, which React renders. Make the left side a real boolean:
export default function App() {
const cart = [];
return (
<div>
<h2>Cart</h2>
{cart.length > 0 && <p>{cart.length} items</p>}
{cart.length === 0 && <p>Your cart is empty</p>}
</div>
);
}
Cart Your cart is empty
Loading, error and empty states
Most screens that show data have four states. Handle each one explicitly, with early returns:
function CourseList({ status, courses, error }) {
if (status === "loading") return <p>Loading courses…</p>;
if (status === "error") return <p role="alert">Error: {error}</p>;
if (courses.length === 0) return <p>No courses match your filters.</p>;
return (
<ul>
{courses.map((c) => <li key={c}>{c}</li>)}
</ul>
);
}
export default function App() {
return (
<>
<CourseList status="loading" courses={[]} />
<CourseList status="error" courses={[]} error="Network request failed" />
<CourseList status="done" courses={[]} />
<CourseList status="done" courses={["React.js", "Next.js"]} />
</>
);
}
Loading courses… Error: Network request failed No courses match your filters. • React.js • Next.js
Choosing a style
- if / early return – whole-component alternatives (loading, error, not logged in).
- ternary – one of two things inside JSX.
- && – something or nothing; keep the condition boolean.
- For three or more branches, use an object lookup or move the logic into a small component.
Practice
Try it yourself
Write a
StockLabelthat shows "Out of stock" for 0, "Only N left" for 1–5 and "In stock" otherwise.Show solution
function StockLabel({ qty }) { if (qty === 0) return <p>Out of stock</p>; if (qty <= 5) return <p>Only {qty} left</p>; return <p>In stock</p>; } export default function App() { return ( <> <StockLabel qty={0} /> <StockLabel qty={3} /> <StockLabel qty={40} /> </> ); }Out of stock Only 3 left In stock
Quick check
How do you render nothing from a component?
Return null.
Why can {items.length && <List />} show "0"?
When length is 0 the expression is 0, and React renders numbers. Use items.length > 0.
Can you write if inside JSX braces?
No – use a ternary, &&, or compute the value before the return.
Summary
- Use if and early returns for whole alternatives, null to render nothing.
- Use the ternary inside JSX, and && with a boolean condition.
- Handle loading, error, empty and success states explicitly.
Finished this lesson?
Mark it complete to track your progress, then carry on with the next lesson.