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

154
Views
¿Cómo hago que el detector de eventos de Windows se active una vez por pulsación de tecla?

Actualmente tengo un componente de reacción y estoy tratando de tener un detector de eventos de Windows que se active en cada pulsación de tecla y reaccionará si es la tecla de tabulación, pero actualmente está presionando varias veces cada vez.

¿Hay alguna forma de hacerlo de modo que cada pulsación de tecla se active una sola vez? Aquí está el código

 render() { window.addEventListener('keydown',(event) => { // here the eventListener should only trigger once per keypress, but it's hitting 4 times console.log("event",event); }) }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

addEventListener no pertenece a la función render() . Allí creará un nuevo detector de eventos en cada renderizado de ese componente.

En su lugar, colóquelo en el enlace del ciclo de vida componentDidMount .

 class Component extends React.Component{ handleKeyDown =(event) =>{ // here the eventListener should only trigger once per keypress, but it's hitting 4 times console.log("event",event); } componentDidMount(){ window.addEventListener('keydown',this.handleKeyDown) } componentWillUnmount(){ window.removeEventListener('keydown',this.handleKeyDown) } render() { return <button onClick={()=>this.setState((state)=>({test:state?.test+1}))}>test</button> } }

Tenga en cuenta que configurar un detector de eventos en la window creará un detector de eventos global. Debe eliminarlo cuando ya no lo necesite, por ejemplo, en componentWillUnmount

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!