Conditional Rendering
Ternary Operator
When you want to render A when a condition is true
or B if the condition is false
, you can use the ternary operator.
In addition to primitive values like the strings "even"
and "odd"
above, you can also conditionally render JSX and React Components.
Rendering Something Only When a Condition is true
true
Say you want to render the <WelcomeMessage />
component below only when a user is logged in. If they're not logged in, you don't want to render anything at all.
You can use null
on the false
side of the ternary operator
or use the Logical AND operator &&
and its short-circuit behaviour.
Primitive Types JSX Won't Render
null
undefined
Booleans (
true
andfalse
)
Last updated
Was this helpful?