Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

322
Visualizações
React Map function not rendering on browser, but it is console.logging

I'm working on a React homework assignment, working with the pokemon API. In this particular component, I'm accessing the API to return a list of all the pokemon names and render them to the browser.

I've called the API and mapped it out, and it seems to work when I console.log the names, but I'm not sure what I'm doing wrong that it is not rendering onto the actual browser, and could definitely use some help. Code:

import React, { useEffect, useState } from "react";

function PokedexHome(){
    const [pokedexList, setPokedexList] = useState(undefined);
    const [isLoading, setIsLoading] = useState(true);
    const [hasError, setHasError] = useState(false);

    useEffect(() => {
        fetch(`https://pokeapi.co/api/v2/pokemon/?limit=100&offset=0`)
        .then(response => response.json())
        .then(data => {
                setPokedexList(data)
                setIsLoading(false);
            },
            error => {
                setHasError(true)
                setIsLoading(false)
            }
        );
    },[]);

    if(isLoading){
        return <p>Loading...</p>
    }

    if(hasError){
        return <p>An error has occurred, please try again later</p>
    }
    
    pokedexList.results.map((pokemon) => {
        console.log(pokemon.name)
        return <div className="list-container">
             <p>{pokemon.name}</p>
            </div>
           
      })
    };
      


export default PokedexHome

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

if you have a list then your PokedexHome returns void :)

so, first of all, you are missing a return before the map.

second, (if nothing changed lately) you can't return an array of components, you need to return a single component, which can be a Fragment (a React component without UI representation, created for this purpose)

return (
  <>
    {
      pokedexList.results.map((pokemon) => {
        console.log(pokemon.name)
        return <div className="list-container">
             <p>{pokemon.name}</p>
            </div>
           
      })

    }
  </>
)
about 4 years ago · Juan Pablo Isaza Relatório

0

Your component need to return a JSX, so add return and wrap your list with <></>.

import React, { useEffect, useState } from 'react';

function PokedexHome() {
  const [pokedexList, setPokedexList] = useState(undefined);
  const [isLoading, setIsLoading] = useState(true);
  const [hasError, setHasError] = useState(false);

  useEffect(() => {
    fetch(`https://pokeapi.co/api/v2/pokemon/?limit=100&offset=0`)
      .then((response) => response.json())
      .then(
        (data) => {
          setPokedexList(data);
          setIsLoading(false);
        },
        (error) => {
          setHasError(true);
          setIsLoading(false);
        },
      );
  }, []);

  if (isLoading) {
    return <p>Loading...</p>;
  }

  if (hasError) {
    return <p>An error has occurred, please try again later</p>;
  }

  return (
    <>
      {pokedexList.results.map((pokemon) => {
        console.log(pokemon.name);
        return (
          <div className="list-container">
            <p>{pokemon.name}</p>
          </div>
        );
      })}
    </>
  );
}
about 4 years ago · Juan Pablo Isaza Relatório

0

The most probable issue I can think of here is, you are not wrapping the whole JSX in () round braces. It specifies that you want to return something. With main function return statement of course

Example code

Try wrapping it like this.

return  pokedexList.results.map((pokemon) => (
        
         <div className="list-container">
             <p>{pokemon.name}</p>
            </div>
           
      ))
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda