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

160
Visualizações
Removing component with settimeout - React Class Component

I have a notification component, which when rendered, I remove it with some delay.

The problem is that when I try to use clearTimeout to cancel the removal, it doesn't. Look

class Notify extends React.Component {

    constructor(props){
        super(props)
        this.class = 'check'
        this.state = {load:LineNotify[this.class].anime}
        this._delay()
        this.timerS = null
        this.box = BoxNotify[this.class]
        this.icon = IconNotify[this.class]
    }
    remove=()=>{
        // console.log('removed')
        unmountComponentAtNode(document.getElementById('notify'))        
    }

    _delay=()=>{
        this.timerS = setTimeout(this.remove,2400)
    }

    stop=()=>{
        clearTimeout(this.timerS)
        this.setState({load:LineNotify[this.class].line})
    }
    start=()=>{
        this.timerS = setTimeout(this.remove,2400)
        this.setState({load:LineNotify[this.class].anime})
    }

I have functions to control component removal, they must be called with cursor input, ie if user hovers, I should use clearTimeout to cancel removal ("start,stop")

remove=()=>{
    console.log('removed')
    // unmountComponentAtNode(document.getElementById('notify'))        
}

When I don't use unmountComponentAtNode, using clearTimeout works as expected, but when using unmountComponentAtNode, clearTimeout is ignored and the component is removed anyway

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I found a solution. Maybe somehow the timer ID returned by setTimeout is getting lost.

class Notify extends React.Component {

    constructor(props){
        super(props)
        this.class = 'check'
        this.state = {load:LineNotify[this.class].anime}
        this.timerS = this.timer()
        this.box = BoxNotify[this.class]
        this.icon = IconNotify[this.class]
    }

    timer=()=>setTimeout(()=>{
        document.getElementById('notify').remove()
    },2400)

    stop=()=>{
        clearTimeout(this.timerS)
        this.setState({load:LineNotify[this.class].line})
    }
    start=()=>{
        this.timerS = this.timer()
        this.setState({load:LineNotify[this.class].anime})
    }
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