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

330
Visualizações
React: setTimeout not clearing within useEffect hook

Currently my app switches to the /about page after 5s of an intro on the /home page. I want to break this timeout if someone manually changes the page.

For this I have a state of introDisplayed which is false by default. By clicking on a link within the app the introDisplayed is becoming true

Why is the setTimeout within the useEffect not clearing? For reference I have listed all of my attempts on this matter.

clarification: I've tried adding introDisplayed alone to dependencies within useEffect, as well as with history and setIntroDisplayed as suggested by eslint and it doesn't change anything in this case

const Page = ({ introDisplayed, setIntroDisplayed }) => {

  const history = useHistory()

  const location = useLocation()

  // useEffect(() => {
  //   if (introDisplayed === false) {
  //     setTimeout(() => {
  //       setIntroDisplayed(true)
  //       history.push("/about")
  //     }, 5000)
  //   } else clearTimeout()
  // })
  // useEffect(() => {
  //   if (introDisplayed === false) {
  //     var timer = setTimeout(() => {
  //       setIntroDisplayed(true)
  //       history.push("/about")
  //     }, 5000)
  //   } else clearTimeout(timer)
  // })
  useEffect(() => {
    const timer = setTimeout(() => {
      setIntroDisplayed(true)
      history.push("/about")
    }, 1000)
    if (introDisplayed === true) {
      return () => clearTimeout(timer)
    }
  })

  return ()
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I think you've just forgotten to add dependency at useEffect().

useEffect(() => {
  if(introDisplayed === true) return;
  const timer = setTimeout(() => {
    setIntroDisplayed(true)
    history.push("/about")
  }, 1000)
  return () => clearTimeout(timer)
}, [introDisplayed])
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