Margherita
Available
Nikita Goluban
Full-Stack Developer Logistics & Marketing Mechanical & Construction Skills Woodwork & Metalwork Polyglot
React.js + Next.js · Module 2. React Basics · Lesson 7
Conditional rendering allows a React component to display different content depending on a condition.
In Pizza Palace, we may need to show:
React uses normal JavaScript conditions to decide what appears in the interface.
&&.? :.false, null, and undefined.REACT · 01
Conditional rendering means choosing what to display by checking application data.
const availableCount = 10;
function App() {
return (
<div>
<h2>Margherita</h2>
<p>
{availableCount > 0 ? "Available to order" : "Sold out"}
</p>
</div>
);
}
export default App;
availableCount > 0 checks whether at least one pizza can be ordered.
If the condition is true, React displays:
Available to order
Otherwise, it displays:
Sold out
The interface now responds to menu data.
REACT · 02
Use the logical AND operator && when content should appear only if a condition is true.
{condition && <Element />}
Remember: true → render the element; false → render nothing.
const discount = 20;
function App() {
return (
<div>
<h2>Family Pizza</h2>
{discount > 0 && (
<p>Discount: {discount}%</p>
)}
</div>
);
}
export default App;
Because 20 > 0 is true, the discount appears. If discount becomes 0, the paragraph disappears.
The expression must be inside JSX curly braces:
{discount > 0 && <p>Special offer!</p>}
REACT · 03
Sometimes a condition is already stored as a boolean.
const isChefSpecial = true;
function App() {
return (
<div>
<h2>Truffle Pizza</h2>
{isChefSpecial && <p>Chef's special</p>}
</div>
);
}
export default App;
isChefSpecial is true.isChefSpecial already contains true, so this is enough:
isChefSpecial && <p>Chef's special</p>
Writing isChefSpecial === true is valid but unnecessary.
REACT · 04
A pizza-ordering website may greet a returning customer.
const isLoggedIn = true;
function App() {
return (
<div>
<h1>Pizza Palace</h1>
{isLoggedIn && <p>Welcome back! Your saved addresses are ready.</p>}
</div>
);
}
export default App;
If isLoggedIn becomes false, the welcome message disappears.
Conditional rendering controls the visible interface. It does not protect private data by itself.
Authentication, account data, order history, admin actions, and protected prices must also be secured by the backend. Hiding a button in React is not authorization.
REACT · 05
Use the ternary operator when there are two possible results.
{condition ? resultIfTrue : resultIfFalse}
const isLoggedIn = false;
function App() {
return (
<div>
<h1>Pizza Palace</h1>
<p>
{isLoggedIn ? "Welcome back!" : "Log in to view previous orders"}
</p>
</div>
);
}
export default App;
Because isLoggedIn is false, React displays the expression after :.
REACT · 06
Both operators can control rendered content, but they solve different problems.
{hasFreeDelivery && <p>Free delivery!</p>}
If the condition is false, nothing is displayed.
{isAvailable ? (
<button>Add to cart</button>
) : (
<p>Sold out</p>
)}
One of the two branches is rendered.
| Operator | Use it when |
|---|---|
&& |
Something appears only when a condition is true |
? : |
You must choose between two results |
&&; the button is the true ternary branch.REACT · 07
A condition can choose between entire groups of JSX elements.
interface CartSummaryProps {
customerName: string;
items: string[];
}
function CartSummary(props: CartSummaryProps) {
return (
<section>
<h2>Hello {props.customerName}</h2>
{props.items.length > 0 ? (
<>
<p>You have {props.items.length} pizzas in your cart.</p>
<p>Review the order before checkout.</p>
<button>Open cart</button>
</>
) : (
<p>Your cart is empty.</p>
)}
</section>
);
}
export default CartSummary;
Usage:
<CartSummary
customerName="Nikita"
items={["Margherita", "Pepperoni"]}
/>
When the array contains items, React displays two paragraphs and a button. When it is empty, React displays only Your cart is empty.
The Fragment <></> groups several elements without adding another HTML wrapper.
REACT · 08
React does not display false, null, or undefined as visible text when they are JSX children.
function App() {
return (
<div>
{false}
{null}
{undefined}
<p>Choose your pizza!</p>
</div>
);
}
false, null, and undefined add no visible text.Only Choose your pizza! is visible.
A component can return null when it should display nothing.
interface OrderNoticeProps {
visible: boolean;
}
function OrderNotice(props: OrderNoticeProps) {
if (!props.visible) {
return null;
}
return <p>Your order was added to the kitchen queue.</p>;
}
export default OrderNotice;
visible=false, this entire message area is absent.<OrderNotice visible={false} /> displays nothing. With visible={true}, React displays the message.
REACT · 09
Using a number directly on the left side of && can display an unwanted zero.
const cartItems: string[] = [];
function App() {
return (
<div>
{cartItems.length && <p>Your cart contains pizzas.</p>}
</div>
);
}
For an empty array, cartItems.length is 0. The expression returns 0, and React can display it.
{cartItems.length > 0 && <p>Your cart contains pizzas.</p>}
0.The explicit comparison produces true or false.
Rule: when using && with a number, write a boolean comparison such as count > 0.
REACT · 10
Reusable components can receive conditions through props.
interface PizzaCardProps {
name: string;
availableCount: number;
}
function PizzaCard(props: PizzaCardProps) {
return (
<article>
<h2>{props.name}</h2>
<p>
{props.availableCount > 0 ? "Available" : "Sold out"}
</p>
</article>
);
}
export default PizzaCard;
Usage:
<PizzaCard name="Margherita" availableCount={10} />
<PizzaCard name="Truffle Pizza" availableCount={0} />
The same component displays different messages because each instance receives different props.
REACT · 11
Conditions can depend on React state.
import { useState } from "react";
export default function PizzaDetails() {
const state = useState(false);
const showIngredients = state[0];
const setShowIngredients = state[1];
function toggleIngredients() {
setShowIngredients(!showIngredients);
}
return (
<section>
<h2>Margherita</h2>
<button onClick={toggleIngredients}>
{showIngredients ? "Hide ingredients" : "Show ingredients"}
</button>
{showIngredients && (
<p>Tomato, mozzarella, and basil</p>
)}
</section>
);
}
Initially, showIngredients is false. Clicking the button reverses the boolean value, React renders again, and the ingredients appear. Clicking again hides them.
REACT · 12
Conditional rendering does not always require && or a ternary. Ordinary JavaScript if statements are often clearer for early returns.
interface AddToCartButtonProps {
availableCount: number;
}
function AddToCartButton(props: AddToCartButtonProps) {
if (props.availableCount === 0) {
return <p>Sold out</p>;
}
return <button>Add to cart</button>;
}
export default AddToCartButton;
If the count is zero, the function returns the sold-out message immediately. Otherwise, it returns the button.
Use an if statement when it makes the component easier to read.
REACT · 13
At checkout, Pizza Palace can display different instructions for delivery and pickup.
interface FulfilmentMessageProps {
method: "delivery" | "pickup";
address?: string;
}
function FulfilmentMessage(props: FulfilmentMessageProps) {
return (
<section>
<h2>Order method</h2>
{props.method === "delivery" ? (
<p>We will deliver to: {props.address ?? "Address required"}</p>
) : (
<p>Your order will be ready for pickup at Pizza Palace.</p>
)}
</section>
);
}
export default FulfilmentMessage;
Usage:
<FulfilmentMessage
method="delivery"
address="12 Market Street"
/>
This demonstrates UI behavior. A real ordering system must also validate the address and allowed delivery area on the backend.
REACT · 14
Incorrect:
{{availableCount > 0 ? "Available" : "Sold out"}}
Correct:
{availableCount > 0 ? "Available" : "Sold out"}
Incorrect:
{isChefSpecial = true && <p>Chef's special</p>}
Correct:
{isChefSpecial && <p>Chef's special</p>}
The assignment operator = changes a value. It is not a comparison.
Incorrect:
{isAvailable ? "Add to cart"}
Correct:
{isAvailable ? "Add to cart" : "Sold out"}
Incorrect:
{isLoggedIn ? (
<p>Welcome back</p>
<button>View previous orders</button>
) : (
<p>Please log in</p>
)}
Correct:
{isLoggedIn ? (
<>
<p>Welcome back</p>
<button>View previous orders</button>
</>
) : (
<p>Please log in</p>
)}
Potentially problematic:
{cartCount && <p>Items in cart</p>}
Better:
{cartCount > 0 && <p>Items in cart</p>}
REACT · 15
Create const discount = 20 and display Special discount: 20% only when the discount is greater than zero. Use &&.
Create const isLoggedIn = false. Display Welcome back! when true and Please log in when false. Use a ternary.
Create const availableCount = 0. Display Available to order when the count is greater than zero and Sold out otherwise.
Create an OrderNotice component with a visible: boolean prop. Return null when visible is false.
Create a reusable component receiving:
interface PizzaCardProps {
name: string;
price: number;
isAvailable: boolean;
}
Display the pizza name and price. Use isAvailable to show either an Add to cart button or a Sold out message.
Create a state value that switches between delivery and pickup. Render the correct checkout instructions for the selected method.
REACT · 16
&& when content appears only for a true condition.? : to choose between two results.if statement when an early return is easier to read.false, null, or undefined as visible children.null to display nothing.&&.REACT · 17
Next lesson: Rendering Collections in React with .map() and key, using the Pizza Palace menu and cart.