Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

202
Views
El clic del elemento no se activa cuando el elemento se oculta

Tengo un evento de desenfoque en una entrada de texto que activa un botón (lo representa con React). Cuando hace clic en el botón, el evento de desenfoque se activa en la entrada de texto que elimina el botón del DOM, pero el evento de clic del botón no se activa. Intenté envolver el código que elimina el botón del DOM en un tiempo de espera de 100 ms y funciona pero se siente extraño, ¿hay una mejor manera?

Aquí está la esencia del código:

 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 answers
Answer question

0

Esto es fácil, solo necesita usar un disparador de evento diferente. onClick es demasiado tarde, no se activará hasta que ocurra el desenfoque del área de texto, pero onMouseDown se activará antes:

 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 Report

0

Dado que renderizas condicionalmente el botón con:

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

Tan pronto como establezca showInput en un valor falso, se eliminará del DOM. Tienes un par de opciones:

Opción 1 (si no tienes que quitar el botón del DOM)

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

Establecer la opacidad en 0 ocultará el botón, pero no lo eliminará del dom.

Opción 2 (Si tienes que quitar el botón del 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!