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

295
Views
Loop through nested axios get request - pagination api response

I want to loop through my api repsonse which has an pagination.

Here is my api response

{
    count: 165,
    next: "http://example.com/name?offset=30&per_page=30",
    previous: null
}

Here is my useEffect

const [datas, setData] = useState([]);

async function getAllData() {
    await axios.get("/names").then((response) => { // what should be the logic here to loop the api response
        setData(response.data.results);
    }).catch((error) => {
        console.log(error);
    });
}

How should be the implementation of this ?

Thank you

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

This code will make the inital request and will keep requesting the "next" endpoint provided by the API until it is no longer there, meaning that you have parsed all the data.

const [data, setData] = useState([]);    

async function requestData()
{
    let nextUrl = "/names";
  
    do {
        const response = await axios.get(nextUrl);
        nextUrl = response.data.next;
    
        setData([...data, ...response.data.results]);
    
    } while(nextUrl != null)
}

about 4 years ago · Santiago Trujillo 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!