Lists & Conditions Lesson 8 of 13
React Conditional Rendering
Show different content depending on state or props with if statements, the ternary operator, the && operator, and returning null.
Real interfaces change with the situation: show "Log in" or "Log out", a loading message, an error, or nothing at all. In React, you decide what to show with plain JavaScript conditions.
if with early return
<div id="root"></div>
<script src="/vendor/react.min.js"></script>
<script src="/vendor/react-dom.min.js"></script>
<script src="/vendor/babel.min.js"></script>
<script type="text/babel">
const { useState } = React;
function Greeting({ user }) {
if (!user) {
return <p>Please log in to see your grades.</p>;
}
return <p>Welcome back, <strong>{user}</strong>! 👋</p>;
}
function App() {
const [user, setUser] = useState(null);
return (
<div style={{ fontFamily: "sans-serif", padding: 16 }}>
<Greeting user={user} />
{user
? <button onClick={() => setUser(null)}>Log out</button>
: <button onClick={() => setUser("Ana")}>Log in as Ana</button>}
</div>
);
}
ReactDOM.createRoot(document.getElementById("root")).render(<App />);
</script>
The ternary operator
Inside JSX you can't write if, but you can use the ternary condition ? a : b:
<p>{grade >= 75 ? "PASSED ✅" : "FAILED ❌"}</p>
<span className={grade >= 75 ? "pass" : "fail"}>{grade}</span>
&& for "show or nothing"
<div id="root"></div>
<script src="/vendor/react.min.js"></script>
<script src="/vendor/react-dom.min.js"></script>
<script src="/vendor/babel.min.js"></script>
<script type="text/babel">
function Product({ name, stock, isNew }) {
return (
<li style={{ margin: "6px 0" }}>
{name}
{isNew && <span style={{ background: "gold", padding: "0 6px", marginLeft: 6, borderRadius: 6 }}>NEW</span>}
{stock === 0 && <span style={{ color: "crimson", marginLeft: 6 }}>Sold out</span>}
{stock > 0 && stock < 10 && <span style={{ color: "darkorange", marginLeft: 6 }}>Only {stock} left!</span>}
</li>
);
}
function App() {
return (
<ul style={{ fontFamily: "sans-serif" }}>
<Product name="Ube pandesal" stock={30} isNew />
<Product name="Chips" stock={0} />
<Product name="Ensaymada" stock={4} />
</ul>
);
}
ReactDOM.createRoot(document.getElementById("root")).render(<App />);
</script>
condition && <Element /> shows the element only when the condition is true. A prop written without a value, like isNew, is true.
Returning null
A component can return null to show nothing:
function Alert({ message }) {
if (!message) return null;
return <div className="alert">{message}</div>;
}
Loading, error and success
<div id="root"></div>
<script src="/vendor/react.min.js"></script>
<script src="/vendor/react-dom.min.js"></script>
<script src="/vendor/babel.min.js"></script>
<script type="text/babel">
const { useState } = React;
function App() {
const [status, setStatus] = useState("idle"); // idle | loading | error | done
function load(fail) {
setStatus("loading");
setTimeout(() => setStatus(fail ? "error" : "done"), 1000);
}
return (
<div style={{ fontFamily: "sans-serif", padding: 16 }}>
<button onClick={() => load(false)}>Load grades</button>{" "}
<button onClick={() => load(true)}>Simulate error</button>
{status === "loading" && <p>⏳ Loading…</p>}
{status === "error" && <p style={{ color: "crimson" }}>❌ Could not load grades. Try again.</p>}
{status === "done" && <p>✅ Math 88 · Science 92 · English 85</p>}
</div>
);
}
ReactDOM.createRoot(document.getElementById("root")).render(<App />);
</script>
Using one status value instead of several booleans (isLoading, hasError…) prevents impossible combinations like "loading and error at the same time".
Quick Quiz
Check what you learned. Walang grade ito, practice lang. 🙂
By Sir Reyel · Last updated 2026-12-04