Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

79
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!