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

280
Vistas
useEffect calls stop working when called too many times. Why?

I have a useEffect, which works properly when done like this.

    async function readUserPosts(profileID){
        try {
            const res = await axios.post(...);
            setUserPosts(res.data.posts);
        } catch (err) {
            console.error(err);
        }
    }

    useEffect(() => {
        readUserPosts(profileID);   
    }, [profileID])

But, if I define the same useEffect by adding a second function like this:

    async function readUserPosts(profileID){
        try {
            const res = await axios.post(...);
            setUserPosts(res.data.posts);
        } catch (err) {
            console.error(err);
        }
    }

    useEffect(() => {
        readUserPosts(profileID);
        functionX();
    }, [profileID])

The useEffect works the first few times I reload the page, after which it stops working. I do not get any error message, even when I wrap the try catch around the 2 functions. FunctionX is a simple async function. I have substituted other test functions on functionX, and it produces the same result. So basically, it will not work when a second function is defined. What went wrong?

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

0

Enclose the two methods with useCallback. Here is one method for example:

const readUserPosts = useCallback(async function() {
  try {
        const res = await axios.post(...);
        setUserPosts(res.data.posts);
    } catch (err) {
        console.error(err);
    }
 }, []);

And try with adding dependency like this:

useEffect(() => {
    readUserPosts(profileID);
    functionX();
}, [profileID, readUserPosts, functionX])
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