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'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.
.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.
Item added/removed/reordered
Matches old → new by key
Only changed items re-render
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.
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.
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.
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.
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.
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.
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.
Test your knowledge
{isLoggedIn && <Dashboard />} render when isLoggedIn is false?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 →