codex-lv3-may-2025

Kata 14: Fetch Pokemon and Display Moves

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

Challenge

Create a PokemonMoves component that:

  1. Fetches a pokemon from: https://pokeapi.co/api/v2/pokemon/pikachu
  2. Uses a for loop in useEffect to extract all move names from the pokemon
  3. Stores the move names in state
  4. Uses .map() in JSX to display all moves as a list

🔗 Practice on CodeSandbox

Expected Output

When loaded, displays all move names:

Moves:
- thunder-shock
- quick-attack
- thunderbolt
- ... (and more)

Starter Code

import { useState, useEffect } from 'react';

export default function PokemonMoves() {
  const [moveList, setMoveList] = useState([]);
  
  useEffect(() => {
    async function fetchPokemon() {
      const response = await fetch('https://pokeapi.co/api/v2/pokemon/pikachu');
      const data = await response.json();
      
      // MAP: Use for loop to extract move names
      const moveNames = [];
      // Your for loop here
      
      setMoveList(moveNames);
    }
    
    fetchPokemon();
  }, []);
  
  if (moveList.length === 0) {
    return <div>Loading...</div>;
  }
  
  return (
    <div>
      <h3>Moves:</h3>
      <ul>
        {moveList.map((move) => (
          <li key={move}>{move}</li>
        ))}
      </ul>
    </div>
  );
}

Hints

Solution

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

export default function PokemonMoves() {
  const [moveList, setMoveList] = useState([]);
  
  useEffect(() => {
    async function fetchPokemon() {
      const response = await fetch('https://pokeapi.co/api/v2/pokemon/pikachu');
      const data = await response.json();
      
      // MAP: Use for loop to extract move names
      const moveNames = [];
      for (let i = 0; i < data.moves.length; i++) {
        moveNames.push(data.moves[i].move.name);
      }
      
      setMoveList(moveNames);
    }
    
    fetchPokemon();
  }, []);
  
  if (moveList.length === 0) {
    return <div>Loading...</div>;
  }
  
  return (
    <div>
      <h3>Moves:</h3>
      <ul>
        {moveList.map((move) => (
          <li key={move}>{move}</li>
        ))}
      </ul>
    </div>
  );
}

Concept Review

Using For Loop for Data Transformation

Using .map() Method for Rendering

Using Move Name as Key

Why Use Both a for loop and .map()?

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

Challenge Variation

Try mapping other pokemon data:


← Back to Kata Index