codex-lv3-may-2025

Level Navigation: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19⚡ 20

Level 7: Display Data with For Loop

Goal: Display the fetched meals using a for loop.

User Story: As a user, I want to see the meals displayed on the page so that I can view what’s planned for the potluck.


What You’ll Do

Add display logic using a for loop to render each meal in the list.

Instructions

💡 Code Hints

Need help with the display loop? Check out these snippets:

Show Me: display loop
const mealsDisplay = []
for (let i = 0; i < meals.length; i++) {
    mealsDisplay.push(
        <li key={meals[i].id}> 
            {meals[i].meal_name} by {meals[i].guest_name} serves {meals[i].serves} ( {meals[i].kind_of_dish} ) 
        </li>
    )
}
Show Me: displaying the meals
<ul>
    {mealsDisplay}
</ul>
Show Me: Meals displayed in app Meals displayed in app

🔍 Diving Deeper

Why do we need the key prop?

Understanding JSX in loops:

mealsDisplay.push(
    <li key={meals[i].id}> 
        {meals[i].meal_name} by {meals[i].guest_name} serves {meals[i].serves} ( {meals[i].kind_of_dish} ) 
    </li>
)

Alternative approaches:

You could also use .map() instead of a for loop:

const mealsDisplay = meals.map(meal => (
    <li key={meal.id}>
        {meal.meal_name} by {meal.guest_name} serves {meal.serves} ({meal.kind_of_dish})
    </li>
));

📺 Learn More:

✅ Check

  1. Click the “Fetch Meals” button
  2. Your meals should appear in a list below the button
  3. Each meal shows: name, guest, serves count, and dish type
  4. The list updates when you click the button again
  5. If no data appears, check your Supabase connection and RLS policies

📚 Vocabulary