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

216
Visualizações
How do I create SyntheticEvent listener on the document?

In the website I'm currently developing, native events (regular KeyboardEvents) produce unwanted behaviour (using old versions of state and refs), whilst SyntheticEvents (which I believe are React.KeyboardEvents) work flawlessly. I'm trying to put a SyntheticEvent onKeyDown listener on the document (so that the keypress can be handled no matter where the focus is), is there any way to achieve this? To work around this, I've made a wrapper element across the whole app with a onKeyDown listener, this does not work well though, because it does not register all the presses (such as when a hamburger menu or a full screen photo is focused, which I think happens due to their absolute/fixed positioning).

Example of using document.addEventListener("keydown", handleKeyDown), which fires native element, but has incorrect behaviour (handleKeyDown uses outdated refs), but registers keypresses globally:

  • Stack Trace: Stack trace of addEventListener
  • Event Type: Event type addEventListener

Example of using app wrapper in which handleKeyDown works correctly (the refs are current), probably because it is a SyntheticEvent. It does not register clicks globally though:

  • Stack Trace: Stack trace of app wrapper
  • Event Type: Event type of app wrapper
about 4 years ago · Juan Pablo Isaza
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