Because JSX curly braces can only hold expressions, not statements, an if/else block can't be written directly inline inside JSX the way a ternary or logical && can — instead, the if/else logic runs earlier in the component's function body, assigning the JSX to render into a variable, or an early return statement returns entirely different JSX before the rest of the component even runs. This approach tends to be the clearest, most readable choice specifically when the conditional logic itself is non-trivial, involving several branches or conditions, since forcing complex branching into a single inline ternary or && expression can quickly become hard to read.
1Understanding Conditional Rendering with if/else
Because JSX curly braces can only hold expressions, not statements, an if/else block can't be written directly inline inside JSX the way a ternary or logical && can — instead, the if/else logic runs earlier in the component's function body, assigning the JSX to render into a variable, or an early return statement returns entirely different JSX before the rest of the component even runs. This approach tends to be the clearest, most readable choice specifically when the conditional logic itself is non-trivial, involving several branches or conditions, since forcing complex branching into a single inline ternary or && expression can quickly become hard to read.
Reach for an if/else block, assigning to a variable, or an early return, instead of a nested ternary once conditional rendering logic involves more than a simple two-way choice — deeply nested ternaries inside JSX are notoriously hard to read correctly.
function Status({ isLoading, error, data }) {
let content;
if (isLoading) {
content = <p>Loading...</p>;
} else if (error) {
content = <p>Error: {error}</p>;
} else {
content = <p>Data: {data}</p>;
}
return <div>{content}</div>;
}2Practical Example
Here is a real-world application of Conditional Rendering with if/else showing how it is used in production React code.
function UserGreeting({ user }) {
if (!user) {
return <p>Please log in.</p>;
}
return <p>Welcome back, {user.name}!</p>;
}3Best Practices
Follow these guidelines when working with Conditional Rendering with if/else:
1. Use an if/else block, assigning the result to a variable before the return, or via an early return, for conditional logic more complex than a straightforward two-way choice
2. Use an early return at the top of a component for a clear guard-clause case, like returning null or a loading indicator before the rest of the component's logic runs
3. Reserve the ternary and logical && operators for genuinely simple, inline conditional expressions, switching to if/else once nesting or complexity grows
Tip: Reach for an if/else block, assigning to a variable, or an early return, instead of a nested ternary once conditional rendering logic involves more than a simple two-way choice — deeply nested ternaries inside JSX are notoriously hard to read correctly.
function Status({ isLoading, error, data }) {
let content;
if (isLoading) {
content = <p>Loading...</p>;
} else if (error) {
content = <p>Error: {error}</p>;
} else {
content = <p>Data: {data}</p>;
}
return <div>{content}</div>;
}