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

220
Visualizações
Registering (and Unregistering) Multiple Hotkeys on Same Page with useMousetrap Hook?

I have a component that uses react hooks to register hotkeys: useMousetrap.

That's a hooks-wrapper for the mousetrap library.

Here is the component:

const MyComponent = ({ name }) => {
    const sayHi = () => {
        alert(`Hello from Component ${name}`);
    };

    useMousetrap('x', sayHi);

    return (
        <div>
            Component {name}
        </div>
    );
};

When I implement that component inside some other component or page, everything works fine. But now say I want to have two components on the same page:

<div>
    <MyComponent name="A" />
    <MyComponent name="B" />
</div>

Now both components try to register the hotkey. It seems like it's always the one that appears later in the document flow, that succeeds. So if I press x in the example above, the alert would say Hello from Component B.

Is there a way to make sure that only one component is ever active? I was thinking of registering and unregistering those hotkeys depending on which component is in view, but useMousetrap being a hook seems to make conditional logic quite difficult. I am bit confused here.

Is there a quick & easy way I can turn the hotkeys on/off conditionally?

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