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

259
Views
Usando ganchos de reacción, ¿cómo escribiría un detector de eventos dependiente del estado que no necesita agregarse y eliminarse cada vez que cambia el estado?

El siguiente código muestra una implementación funcional, pero ineficiente, de un BackHandler de Android dentro de React Native para que la aplicación salga después de dos pulsaciones en dos segundos. Esto se implementa utilizando ganchos React dentro del componente funcional principal de la aplicación.

Sin embargo, debido a la dependencia de una variable de estado recentlyPressedHardwareBack , el useEffect se limpiará y luego se ejecutará cada vez que cambie el estado, lo que hará que el detector de eventos BackHandler se desconecte y se vuelva a conectar cada vez que se presione el botón Atrás. ¿Cómo configura este detector de eventos solo una vez sin creación y eliminación constantes, mientras le permite acceder a un estado de componente cambiante?

 const [recentlyPressedHardwareBack, setRecentlyPressedHardwareBack] = useState(false); useEffect(() => { const backHandler = BackHandler.addEventListener( 'hardwareBackPress', () => { // Exit app if user pressed the button within last 2 seconds. if (recentlyPressedHardwareBack) { return false; } ToastAndroid.show( 'Press back again to exit the app', ToastAndroid.SHORT, ); setRecentlyPressedHardwareBack(true); // Toast shows for approx 2 seconds, so this is the valid period for exiting the app. setTimeout(() => { setRecentlyPressedHardwareBack(false); }, 2000); // Don't exit yet. return true; }, ); return () => backHandler.remove(); }, [recentlyPressedHardwareBack]);
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Podrías usar useRef para esto.

 const recentlyPressedHardwareBackRef = useRef(false); useEffect(() => { const backHandler = BackHandler.addEventListener( 'hardwareBackPress', () => { // Exit app if user pressed the button within last 2 seconds. if (recentlyPressedHardwareBackRef.current) { return false; } ToastAndroid.show( 'Press back again to exit the app', ToastAndroid.SHORT, ); recentlyPressedHardwareBackRef.current = true; // Toast shows for approx 2 seconds, so this is the valid period for exiting the app. setTimeout(() => { recentlyPressedHardwareBackRef.current = false; }, 2000); // Don't exit yet. return true; }, ); return () => backHandler.remove(); }, [])
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!