codex-lv3-may-2025

Kata 15: Fetch All Pokemon and Display Names and Height

Concept: Map pattern with fetched array data (using for loop)

Challenge

Create a PokemonList component that:

  1. Fetches all pokemon from: https://pokeapi.co/api/v2/pokemon?limit=20
  2. The response has a results array with pokemon data
  3. Each pokemon in results needs to be fetched individually (use results[i].url)
  4. Use a for loop to map over all pokemon and collect their names and heights
  5. Display a list showing each pokemon’s name and height

🔗 Practice on CodeSandbox

Expected Output

Displays a list like:

Pokemon List:
- bulbasaur: height 7
- ivysaur: height 10
- venusaur: height 20
... (and more)

Starter Code

import { useState, useEffect } from 'react';

export default function PokemonList() {
  const [pokemonList, setPokemonList] = useState([]);
  
  useEffect(() => {
    async function fetchAllPokemon() {
      // Step 1: Fetch list of pokemon
      const response = await fetch('https://pokeapi.co/api/v2/pokemon?limit=20');
      const data = await response.json();
      
      // Step 2: MAP: Fetch each pokemon and extract id, name, and height using for loop
      const pokemonData = [];
      // Your for loop here
      
      setPokemonList(pokemonData);
    }
    
    fetchAllPokemon();
  }, []);
  
  if (pokemonList.length === 0) {
    return <div>Loading...</div>;
  }
  
  return (
    <div>
      <h3>Pokemon List:</h3>
      <ul>
        {pokemonList.map((pokemon) => (
          <li key={pokemon.id}>{pokemon.name}: height {pokemon.height}</li>
        ))}
      </ul>
    </div>
  );
}

Hints

Solution

Click to reveal solution
import { useState, useEffect } from 'react';

export default function PokemonList() {
  const [pokemonList, setPokemonList] = useState([]);
  
  useEffect(() => {
    async function fetchAllPokemon() {
      // Step 1: Fetch list of pokemon
      const response = await fetch('https://pokeapi.co/api/v2/pokemon?limit=20');
      const data = await response.json();
      
      // Step 2: MAP: Fetch each pokemon and extract id, name, and height using for loop
      const pokemonData = [];
      for (let i = 0; i < data.results.length; i++) {
        const pokemonResponse = await fetch(data.results[i].url);
        const pokemon = await pokemonResponse.json();
        pokemonData.push({
          id: pokemon.id,
          name: pokemon.name,
          height: pokemon.height
        });
      }
      
      setPokemonList(pokemonData);
    }
    
    fetchAllPokemon();
  }, []);
  
  if (pokemonList.length === 0) {
    return <div>Loading...</div>;
  }
  
  return (
    <div>
      <h3>Pokemon List:</h3>
      <ul>
        {pokemonList.map((pokemon) => (
          <li key={pokemon.id}>{pokemon.name}: height {pokemon.height}</li>
        ))}
      </ul>
    </div>
  );
}

Concept Review

Using For Loop for Data Transformation

Using .map() Method for Rendering

Understanding Unique Keys in React

Why Use Both?

Note: Learn more about the built-in .map() method at MDN: Array.map()

Challenge Variation

Try:


Back to Kata Index