After building countless React and React Native components, I’ve run into one too many frustrating bugs caused by the logical AND (&&
) operator used for conditional rendering in JSX. These issues are often subtle and hard to track down, so I’ve made it a rule: I don’t use logical AND for conditional rendering in JSX anymore. Instead, I stick with the ternary (conditional) operator (in other words, shorthand “if” statement). It’s safer, clearer, and avoids nasty surprises.
The Problem with Logical AND for Conditional Rendering
Here’s a typical use case with logical AND:
{user.age && <Text>{`Age: ${user.age}`}</Text>}
You might expect this to only render the Text
element if user.age
is defined, or above 0 (since 0 is a falsy value in JavaScript).
But consider what happens if user.age
is 0
(a valid, real-world age). We don’t want the <Text>
element to get rendered in this case, and it doesn’t.
However, it creates a pretty unwanted side effect:
0 && <Text>...</Text>
evaluates to0
.- React will render the value
0
directly, essentially interpreting it as text instead of a boolean value!- In React Native, this is even worse: it will actually crash the entire application with an error about trying to render text outside of a
<Text>
component!
- In React Native, this is even worse: it will actually crash the entire application with an error about trying to render text outside of a
Example:
const user = { age: 0 };
return (
<View>
{user.age && <Text>{`Age: ${user.age}`}</Text>}
</View>
);
What renders: Just 0
. Not the <Text>
element.
This happens because &&
doesn’t enforce a boolean context; it returns the first falsy value or the last truthy value. That means non-boolean values like 0
or ""
can sneak through and show up unexpectedly. This can also crash your entire application in the case of React Native!
A Better Alternative: The Ternary Operator (Shorthand “if” Statements)
Instead, I use the ternary (conditional) operator, which makes the intent clearer and avoids rendering unwanted values:
{user.age ? <Text>{`Age: ${user.age}`}</Text> : null}
This guarantees that only a JSX element or null
will be rendered; no accidental numbers or strings appearing in your layout.
Improved Example:
const user = { age: 0 };
return (
<View>
{user.age ? <Text>{`Age: ${user.age}`}</Text> : null}
</View>
);
What renders: Nothing if age
is undefined
, null
or 0
, and the full Text
block if it’s any other number.
Summary: Use the Ternary Operator for Conditional Rendering in React
While using &&
for conditional rendering may seem like a shortcut, it’s not always safe. This is especially true when your condition might evaluate to a falsy non-boolean like 0
. From my experience, using the ternary operator leads to fewer bugs and a more predictable UI.
✅ Use:
condition ? <Component /> : null
🚫 Avoid:
condition && <Component />
When writing JSX in React or React Native, I choose clarity over cleverness. The ternary operator keeps my components clean and my debugging sessions short!
Leave a Reply