1. Rendering lists with map()

In UrbanCart, the product page doesn't render one card at a time by hand. It takes an array of products and transforms each one into a <ProductCard /> using JavaScript's .map() method. This is how every React app renders lists.

🛒
UrbanCart Example
Rendering the product grid

UrbanCart's catalog page receives an array of products from the API. Instead of writing 50 separate <div> blocks, we call products.map() to loop through the array and return a JSX element for each item. React renders the returned array of elements as sibling DOM nodes.

ProductList.jsx Rendering with map()
Always return JSX from map(). The callback inside .map() must return a JSX element. If you forget the return (or the parentheses around JSX after an arrow), React renders nothing and you'll stare at a blank page wondering what happened.

2. The key prop

When you render a list, React needs a way to identify each item. That's what the key prop does. Without keys, React has to guess which items changed — and it guesses wrong.

Why Keys Matter
Array Changes
Item added/removed/reordered
→
React Checks Keys
Matches old → new by key
→
Minimal DOM Updates
Only changed items re-render
keys.jsx Correct vs. Incorrect Keys
✅

Good keys

Database IDs (product.id), UUIDs, or any value that is unique and stable across re-renders.

❌

Bad keys

Array index, Math.random(), or any value that changes every render. These cause React to destroy and recreate DOM nodes unnecessarily.

When can you use index? Only if the list is static — it never gets reordered, filtered, or modified. Even then, a stable ID is safer. In UrbanCart, products get filtered by category, sorted by price, and removed from cart — index will break all of those.

3. Conditional rendering with &&

Sometimes you want to show something only if a condition is true. The short-circuit pattern with && is the simplest way to do this in JSX.

StockBadge.jsx Short-Circuit with &&
Watch out for 0! If count is 0, writing {count && <Items />} renders the number 0 on screen — not nothing. Use {count > 0 && <Items />} instead. The left side of && must be a boolean.

4. Conditional rendering with ternary

When you need to show one thing or another — not just show-or-hide — use the ternary operator. This is the "if/else" of JSX.

ProductStatus.jsx Ternary for If/Else
Don't over-nest ternaries. One level of ternary is fine. Two levels is pushing it. If you find yourself writing a ternary inside a ternary inside a ternary, extract a component or use early return instead.

5. Early return

The early return pattern handles edge cases at the top of your component before the main render. It's a guard clause — check for the "nothing to show" case first, then render the happy path.

EmptyState.jsx Guard Clause with Early Return
Early return keeps JSX clean. Instead of wrapping the entire return in a giant ternary, handle edge cases first with if (...) return. The remaining code is always the "normal" case — easy to read, easy to maintain.

6. Showing/hiding components

Combine useState with conditional rendering to toggle UI elements on and off. This is how UrbanCart shows and hides the cart sidebar, filter panels, and modals.

CartSidebar.jsx Toggle with State
🎛️

Conditional render vs. CSS display:none

With {show && <Component />}, React unmounts the component entirely. State inside it resets. With CSS display:none, the component stays mounted — state persists but the DOM is heavier.

💡

When to use which

Use conditional rendering for most cases (modals, sidebars, tabs). Use CSS hiding only when you need to preserve internal state — like a video player that shouldn't restart when toggled.

7. Try It Yourself — Product Grid with Category Filter

Build a product grid with category filter tabs. Click a tab to filter products by category. Show an empty state when no products match. This combines lists, keys, state, and conditional rendering.

⚛️ Try It Yourself — Category Filter
Output

8. Try It Yourself — Shopping Cart

Build a shopping cart with conditional rendering: an empty cart message when there are no items, an item count badge, and a running total. Remove items from the cart and watch the UI update.

⚛️ Try It Yourself — Shopping Cart
Output

Test your knowledge

Question 1 of 5
What method do you use to render a list of elements from an array in React?
Question 2 of 5
Why should you avoid using array index as a key in React lists?
Question 3 of 5
What does {isLoggedIn && <Dashboard />} render when isLoggedIn is false?
Question 4 of 5
What is the difference between the ternary operator and the && pattern for conditional rendering?
Question 5 of 5
What does the "early return" pattern do in a React component?
0
/5
Great job!

Up Next

Lesson 19: Forms in React →
Handle user input, controlled components, and form validation in React.
Africa & India's Best AI Academy
🤖 AI-first 🎓 Placement Assistance

You just rendered dynamic lists and conditional UI.
Now build production React apps.

Join the Best AI Programs — three 5-month courses, fully online, with placement assistance. Week 1 is free.

Explore our AI-era courses →