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

210
Visualizações
How can I fire an event using onLongPress in React native?

I'm using react-native! I want to use TouchableOpacity so that when a button is pressed for about 0.5 seconds, the hellofunc function is executed and an alert('Button Long Pressed'); is displayed.

However, using my code, I have to click and hold the button twice instead of once to run it.

How can I fix my code?

this is my code

const App = () => {
  const hellofunc = () => {
    alert('Button Long Pressed');
  };
  return (
   <TouchableOpacity style={styles.main} onLongPress={hellofunc}>
      <Text>hi</Text>
   </TouchableOpacity>
  );
};

const styles = StyleSheet.create({
  main: {
   backgroundColor: 'lightblue',
   width: '100%',
   height: 30,
  },
});

export default App;
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

There is no direct way, but you can add two events keyUp and keyDown. Make a variable for time when the time taken is more than 0.5 you can set status as long pressed key.

let timeElapsed = 0;
let status = null;

document.addEventListener('keydown', checkLongPress);
document.addEventListener('keyUp', checkLongPress);

const getTimeMS = new Date().getTime(); // in ms

function checkLongPress() {
    timeElapsed = timeElapsed === 0 getTimeMS() ? : getTimeMS() - timeElapsed;
    
    if (timeElapsed /1000 >= threshold) { 
        status = 'Long Pressed';
        timeElapsed = 0;
    }
}
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