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

182
Visualizações
Array data not mappable even though console shows data

I am having a small issue that I cannot seem to get right. I am fetching data from Pokeapi to get some pokemon back, then iterating through that data in order to trigger another fetch request to bring back the associated data with a given pokemon.

I want to push this data to an array so that I can fire off this request as the application loads and save the data to storage so no new requests have to be made. However... (Which I have not done yet btw)

I console.log the results and the data is there, but when trying to map the data out, nada.

Please see my attempt below:

export function SomeComponent() {
  let arr = [];


  async function fetchPokemon() {
  const pokemon = await fetch('https://pokeapi.co/api/v2/pokemon?limit=10')
   .then(response => response.json())
  for await (const x of pokemon.results.map(async (result) => await fetch(result.url)
  .then(response => response.json()))) {
    arr.push(x);
    }
  }

  fetchPokemon();
  console.log(arr)

return (
  <>
    {arr.map((pokemon) => (
       <p>
         {pokemon.name} 
       </p>
    ))}
  </>
 )
}

My best guess is that the console is displaying live results and that they data is not actually populated yet?

I would like to see the pokemon names displayed in the

tag.

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

0

In react we should use state to store data and useEffect to do side effect actions like fetching data :

export function SomeComponent() {
  const [arr, setArray] = useState([]);


  async function fetchPokemon() {
  const pokemon = await fetch('https://pokeapi.co/api/v2/pokemon?limit=10')
   .then(response => response.json())
  for await (const x of pokemon.results.map(async (result) => await fetch(result.url)
  .then(response => response.json()))) {
    setArray([...arr, x]);
    }
  }
useEffect(() => {fetchPokemon()}, []}

return (
  <>
    {arr.map((pokemon) => (
       <p>
         {pokemon.name} 
       </p>
    ))}
  </>
 )
}
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