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

209
Views
¿Cómo puedo activar un evento usando onLongPress en React native?

¡Estoy usando react-native! Quiero usar TouchableOpacity para que cuando se presione un botón durante aproximadamente 0,5 segundos, se ejecute la función hellofunc y una alerta ('Botón presionado durante mucho tiempo'); se visualiza.

Sin embargo, usando mi código, tengo que hacer clic y mantener presionado el botón dos veces en lugar de una vez para ejecutarlo.

¿Cómo puedo arreglar mi código?

este es mi codigo

 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 answers
Answer question

0

No hay una forma directa, pero puede agregar dos eventos keyUp y keyDown. Cree una variable para el tiempo cuando el tiempo empleado sea superior a 0,5, puede establecer el estado como 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 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!