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

161
Vistas
infinite api calls within few seconds Reactjs

I have a Home component in which I call an api, get the data and store in in a state. And finally display the data in an arranged way. Here is the code

import React, { useContext } from "react";
import styled from "styled-components";
//import context
import MainContext from "../context/MainContext";
//api
import { homeDataURL, topSearchesURL } from "../api/base";
import { getResponse } from "../api";

const Home = () => {
  const { topSearches, setTopSearches } = useContext(MainContext);
  getResponse(
     topSearchesURL(),
       (data) => {
         setTopSearches(data);
       },
       (err) => alert(err)
  );  
  return <div></div>;
};

export default Home;

And the getResponse() function

export const getResponse = (url, callback, errcallback) => {
  axios
    .get(url)
    .then((data) => {
      if (callback != null) {
        callback(data.data);
      }
    })
    .catch((err) => {
      if (errcallback != null) {
        errcallback(err);
      }
    });
};

But this function goes to an infinite loop with numerous api hits, i want this to run just one time when the Home component loads.

There is a React hook useEffect which needs a dependecy array, leaving it blank like this

useEffect(() => {
    getResponse(
        topSearchesURL(),
        (data) => {
          setTopSearches(data);
          console.log(data)
        },
        (err) => alert(err)
      );
  }, [])

gives a warning React Hook useEffect has a missing dependency: 'setTopSearches'. Either include it or remove the dependency array react-hooks/exhaustive-deps Removing the array dependency is not an option, so what should I do? Is there any other way to do this?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You are not returning Axios promise, and also no need to use callback use async/await.

export const getResponse = (url) => axios.get(url).then((data) => data.data);

// Component
const apiUrl = topSearchesURL();
useEffect(() => {
  const run = async () => {
    try {
      const data = await getResponse(apiUrl);
      setTopSearches(data);
    } catch (error) {
      alert("Something went wrong");
    }
  };
  run();
}, [apiUrl]);
about 4 years ago · Juan Pablo Isaza 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