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

233
Views
Append To useState<Array> inside useEffect inside a Promise

In theory the array should be populated continuously and should be reflected in rendering the page but instead it keeps getting overwritten. ending with a list that has only the last added value.
This is a striped down version of what I am trying to obtain.

// Just a function to simulate fetching data from an API
async function getData(id) {
    // Simulate Delay
    await new Promise((r) => setTimeout(r, id * 1000));
    return `Data ${id}`;
}

function App(props) {
    const [data, setData] = React.useState("Loading...");
    const [arr, setArr] = React.useState([]);

    React.useEffect(() => {
        getData(0).then(setData);
    }, []);

    React.useEffect(() => {
        if (data && arr.length == 0) {
            for (let i = 1; i < 6; i++) {

                getData(i).then((resolve) => {
                    setArr([resolve, ...arr]);
                });

            }
        } else {
            setArr([]);
        }
    }, [data]);

    return (
        <>
            <h3>{data}</h3>
            <ul>
                {arr.map((item) => (
                    <li key={item}>{item}</li>
                ))}
            </ul>
        </>
    );
}

Expected

Data 0

  • Data 1
  • Data 2
  • Data 3
  • Data 4
  • Data 5

Instead

Data 0

  • Data 5
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

After research I found that you could pass a function to a setState() in the form of (currentStateValue) => {}. To prevent the data from being called twice due to the second useEffect being fired twice, create another state isFetchingMoreData to check against.

// Just a function to simulate fetching data from an API
async function getData(id) {
    // Simulate Delay
    await new Promise((r) => setTimeout(r, id * 1000));
    return `Data ${id}`;
}

function App(props) {
    const [data, setData] = React.useState("Loading...");
    const [arr, setArr] = React.useState([]);
    const [isFecthingMoreData, setIsFecthingMoreData] = React.useState(false);

    React.useEffect(() => {
        getData(0).then(setData);
        setIsFecthingMoreData(true);
    }, []);

    React.useEffect(() => {
        if (isFecthingMoreData) {

            setIsFecthingMoreData(false);

            for (let i = 1; i < 6; i++) 
                getData(i).then((resolve) => {
                setArr((prev )=>[...prev, resolve]);
                });



        } else {
            setArr([]);
        }
    }, [data]);

    return (
        <>
            <h3>{data}</h3>
            <ul>
                {arr.map((item) => (
                    <li key={item}>{item}</li>
                ))}
            </ul>
        </>
    );
}
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!