Render alternatives with if and ternaries
Choose what the user sees
Conditional rendering means choosing a piece of the interface according to a JavaScript value. For example, a parcel collection desk can show different instructions before and after a parcel is ready. React renders the JSX returned for the current inputs. Each complete example in this chapter can replace src/App.jsx in the approved Chapter 3 client project. Expected screen results are predictions, not observed browser output.
function CollectionNote({ ready }) { if (ready) { return <p>Collect your parcel at desk A.</p>; } return <p>Your parcel is being prepared.</p>;}
export default function App() { return ( <main> <h1>Parcel collection</h1> <CollectionNote ready={true} /> <CollectionNote ready={false} /> </main> );}Expected: a heading followed by both instructions, in the order shown. The first call supplies true and takes the early return. The second skips the if body and reaches the last return. The component is reusable because the input determines its output. The ready prop is a boolean, not the string "false", which JavaScript treats as truthy.
Use a ternary when only a small part changes
function DeliveryCard({ express }) { return ( <article> <h2>Delivery option</h2> <p>{express ? 'Next day' : 'Standard delivery'}</p> </article> );}
export default function App() { return <main><DeliveryCard express /><DeliveryCard express={false} /></main>;}Expected: two cards with the same heading and different paragraph text. The expression condition ? valueIfTrue : valueIfFalse selects one value. Braces put that expression into JSX. Use this form when the alternatives are small; duplicating the entire article for one different line makes future edits harder.
Use ordinary JavaScript for several alternatives
function statusLabel(status) { if (status === 'ready') return 'Ready for collection'; if (status === 'transit') return 'On the way'; if (status === 'cancelled') return 'Cancelled'; return 'Status unavailable';}
function ParcelStatus({ status }) { const label = statusLabel(status); return <p>{label}</p>;}
export default function App() { return ( <main> <ParcelStatus status="ready" /> <ParcelStatus status="transit" /> <ParcelStatus status="cancelled" /> <ParcelStatus status="unknown" /> </main> );}Expected: Ready for collection, On the way, Cancelled, Status unavailable. The helper is an ordinary function that returns text; it is not a component. An explicit fallback keeps an unexpected input from being mistaken for a known status. An if statement cannot be placed directly inside JSX braces because braces in this position require an expression. Move the decision before the return, or use a ternary. A branch returning a different element type can also affect component identity; state preservation is taught later, so do not assume every branch preserves state.
Practice
Create a pickup instruction component with a boolean lockersOpen prop and an alternative message for a closed locker. Use an if statement first, then a ternary inside a shared paragraph. Predict both outputs.