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

199
Visualizações
Element click doesn't fire when the element gets hidden

I have a blur event on a text input that toggles on a button (renders it with React). When you click on the button, the blur event fires on the text input which removes the button from the DOM, but the button click event is not fired. I tried wrapping the code that removes the button from the DOM in a 100ms timeout and it works but feels hacky, is there a better way?

Here's the gist of the code:

const blurHandler = e => {
  setShowInput(false); //this prevents buttonHandler from being fired

  // setTimeout(() => setShowInput(false), 100); // this works with 100ms, but not with 50ms for example
}

const buttonHandler = e => {
  console.log('Button clicked!');
}

const focusHandler = e => {
  setShowInput(true);
}

return (
  <div>
    <input type="text" onFocus={focusHandler} onBlur={blurHandler} >
    {showInput && <button onClick={buttonHandler}>Apply to all</button>}
  </div>
)
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

This is easy, you just need to use a different event trigger. onClick is too late, it won't fire until after the textarea blur happens, but onMouseDown will fire before it:

const btn = document.getElementById('btn');
const txt = document.getElementById('txt');

txt.onblur      = () => {console.log("Textarea blur fired")}
txt.onfocus     = () => {console.log("Textarea focus fired")}

btn.onclick     = () => {console.log("Button onClick fired")}
btn.onmousedown = () => {console.log("Button mouseDown fired")}
btn.onmouseup   = () => {console.log("Button mouseUp fired")}
<textarea id="txt">Focus me first</textarea>
<button id="btn">Click me next</button>

about 4 years ago · Juan Pablo Isaza Relatório

0

Since you conditionally render the button with:

{showInput && <button onClick={buttonHandler}>Apply to all</button>}

As soon as you set showInput to a falsy value, it gets removed from the DOM. You have couple of options:

Option 1 (If you don't have to remove the button from the DOM)

return (
  <div>
    <input type="text" onFocus={focusHandler} onBlur={blurHandler} >
    <button style={{opacity: showInput ? 1 : 0}} onClick={buttonHandler}>Apply to all</button>}
  </div>
)

Setting opacity 0 will hide the button, but won't remove it from the dom.

Option 2 (If you have to remove the button from the DOM)

const btnClickedRef = useRef(false)

const buttonHandler = e => {
  console.log('Button clicked!');
  btnClickedRef.current = true
}

return (
  <div>
    <input type="text" onFocus={focusHandler} onBlur={blurHandler} >
    {showInput && btnClickedRef.current && <button onClick={buttonHandler}>Apply to all</button>}
  </div>
)
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