Conditional Rendering
In this page:
{condition ? <ElementA /> : <ElementB />}
{condition && <Element />}
if (!condition) return null;
Using a Ternary for Either/Or Output
The condition ? A : B ternary operator is the standard way to render one of two things in JSX based on a condition.
Example: Using a Ternary for Either/Or Output
<!DOCTYPE html>
<html>
<head>
<script src="https://unpkg.com/react@18/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
</head>
<body>
<div id="root"></div>
<script type="text/babel">
function StatusMessage() {
const isOnline = true;
return <p>{isOnline ? 'You are online' : 'You are offline'}</p>;
}
ReactDOM.createRoot(document.getElementById('root')).render(<StatusMessage />);
</script>
</body>
</html>
Using && to Render Something or Nothing
condition && <Element /> renders the element only when condition is true; when it's false, JSX renders nothing at all for that expression.
Example: Using && to Render Something or Nothing
<!DOCTYPE html>
<html>
<head>
<script src="https://unpkg.com/react@18/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
</head>
<body>
<div id="root"></div>
<script type="text/babel">
function Badge() {
const unread = 3;
return (
<div>
<p>Inbox</p>
{unread > 0 && <span> ({unread} unread)</span>}
</div>
);
}
ReactDOM.createRoot(document.getElementById('root')).render(<Badge />);
</script>
</body>
</html>
Returning null to Render Nothing
A component can return null instead of JSX to render nothing at all -- useful when a component should sometimes not appear in the UI.
Example: Returning null to Render Nothing
<!DOCTYPE html>
<html>
<head>
<script src="https://unpkg.com/react@18/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
</head>
<body>
<div id="root"></div>
<script type="text/babel">
function MaybeMessage() {
const show = false;
if (!show) {
return null;
}
return <p>You will not see this.</p>;
}
ReactDOM.createRoot(document.getElementById('root')).render(<MaybeMessage />);
</script>
</body>
</html>
- Using
&&with a falsy number (like 0), which can accidentally render a stray0on the page. - Writing full if/else blocks directly inside JSX curly braces, which isn't valid.
- Forgetting the ternary operator needs both a true and false branch, unlike
&&.
- Conditional rendering shows different UI based on a condition, using normal JavaScript.
- The ternary operator (
condition ? a : b) picks between two JSX outputs. - The
&&operator renders something only when a condition is true, and nothing otherwise. - Conditions must be expressions, not statements, to work inside JSX.
No browser-specific restrictions -- this relies on standard JavaScript operators.
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: