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

86
Visualizações
Displaying API With React Hooks

I am trying to display a list of facts from this cat fact API https://catfact.ninja/facts?limit=15.

I have fetch the data and see the facts, 0-14, displayed in console. I created a map() to iterate through the array but only the first fact will display on the list. Thank you for your help!

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

const url = 'https://catfact.ninja/facts?limit=15';

  const Index = () => {
    const [factList, setFactList] = useState([]);

    useEffect(() => {
        getList()
    },[]);

    const getList = () => {

        fetch(url)
            .then((res) => res.json())
            .then((list) => {
                console.log('List:', list);
                const [factList] = list.data


                setFactList([factList])
            })
            .catch((err) => {
                console.log('List ERROR ', err);
            })
    }



    return (

        <div className='App'>

            <h1>FACT LIST</h1>
            <ul>

                {factList.map((item, index) => 



                    <li key={index}> {item.fact} </li>



                )}


            </ul>


        </div>
    )
}

export default Index

enter image description here

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

0

Change your fetch function to this:

const getList = () => {
  fetch(url)
    .then((res) => res.json())
    .then((list) => {
      setFactList(list.data);
    })
    .catch((err) => {
      console.log("List ERROR ", err);
    });
};

const [factList] = list.data; causes that only first element of the array is assigned to the factList. Read more about destructuring in JS.

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