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

189
Visualizações
Error while cleaning up onAuthStateChanged inside useEffect

i return the onAuthStateChanged unsubscribe() function to cleanup the useEffect but it doesnt unsubcribe. what may be causing this bug?

useEffect(() => {
        const unsubscribe = onAuthStateChanged(auth, (user) => {
            if(user){
                setInterval(()=>{
                    setUser(user)
                    history.push('/home')

                }, 1500)
            }
            else if(!user){
                setInterval(()=>{
                    history.push('/login')
                    
                }, 1500)
            }
        })
        return ()=> unsubscribe()
    }, [])
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

There might be some problems with the code above, first setInterval is necessary I believe, second, you are returning a closure instead of the function itself, onAuthStateChange returns a function when the state changes so can you try this.

useEffect(() => {
        const unsubscribe = onAuthStateChanged(auth, (user) => {
            if(user){
               setUser(user)
               history.push('/home')
            }
            else {
                history.push('/login')
            }
        });

        return unsubscribe();
    }, [])
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