Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

296
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda