What You'll Learn
You'll learn to recognize when cramming too much conditional logic directly into JSX hurts readability, and how extracting that logic into variables makes your code clearer and easier to debug.
The Problem with Inline Overload
JSX lets you embed JavaScript expressions with curly braces, which is powerful—but that power can backfire. When you chain multiple ternaries, && operators, or complex boolean checks directly inside your JSX, the code becomes a maze. Your return statement should describe what renders, not how you decided it.
Think of it like writing directions: "Turn left if the light is green, otherwise go straight unless it's raining, then turn right" is confusing. Better: "Decide your direction first, then execute the turn."
The Solution: Extract Before JSX
When conditional logic gets complex, compute the result before the return statement:
// ❌ Hard to read
return (
<div>
{user && user.age >= 18 && user.hasPermission ? (
<Dashboard />
) : user && user.age >= 13 ? (
<LimitedView />
) : (
<Restricted />
)}
</div>
);
// ✅ Clear and debuggable
const isAdult = user && user.age >= 18 && user.hasPermission;
const isTeen = user && user.age >= 13;
let content;
if (isAdult) {
content = <Dashboard />;
} else if (isTeen) {
content = <LimitedView />;
} else {
content = <Restricted />;
}
return <div>{content}</div>;
Now you can debug by logging isAdult or isTeen. The JSX shows what renders, not the decision-making process.
When to Extract
If you pause while reading inline logic, extract it. If you can't easily explain what renders in one breath, extract it.
Key Takeaway: Inline conditionals are great for simple cases, but complex logic belongs in variables or if/else blocks before JSX—it keeps your render output readable and your bugs easier to find.