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

106
Visualizações
Need to disable long click on image element

I have the following code on this StackBlitz:

https://stackblitz.com/edit/react-vmstl9

import React from 'react';
import './style.css';

export default function App() {
  return (
    <div>
      <h1>Try on Google Chrome Desktop</h1>
      <p>Open the console log to see how the event gets triggered.</p>
      <p>The event should not get triggered if there is a long click.</p>
      <img
        src="https://via.placeholder.com/200.png/09f/fff"
        onClick={() => {
          console.log('You clicked me!');
        }}
      />
    </div>
  );
}

I need the click event to get triggered only if a normal click happens.

Currently if I click the image and holds the click for 2 seconds, the event still triggers. I need to prevent that.

Any idea on how to achieve that?

Thanks!

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

I found this article on Stack Overflow found here

const myComponent = () => {

    let clickHoldTimer = null;

    const handleMouseDown = () => {
        clickHoldTimer = setTimeout(() => {
            //Action to be performed after holding down mouse
        }, 1000); //Change 1000 to number of milliseconds required for mouse hold
    }

    const handleMouseUp = () => {
        clearTimeout(clickHoldTimer);
    }

    return (
        <div onMouseDown={handleMouseDown} onMouseUp={handleMouseUp} />
    )

}
about 4 years ago · Juan Pablo Isaza Relatório

0

you can do this hacks by get holder Time

https://stackblitz.com/edit/react-d1txdm

export default function App() {
let holdTimer;
return (
  <div>
    <h1>Try on Google Chrome Desktop</h1>
    <p>Open the console log to see how the event gets triggered.</p>
    <p>The event should not get triggered if there is a long click.</p>
    <img
      src="https://via.placeholder.com/200.png/09f/fff"
      onClick={(e) => {
        if (holdTimer > 1000) return;
        else console.log('normal click');
      }}
      onMouseDown={() => {
        holdTimer = new Date().getTime();
      }}
      onMouseUp={() => {
        let endHolder = new Date().getTime();
        holdTimer = endHolder - holdTimer;
       }}
     />
    </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