Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

276
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda