@shankarmorwal/react-native-exit-on-double-press
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -1,2 +0,2 @@ | ||
import {BackHandler, ToastAndroid} from "react-native"; | ||
import {BackHandler, ToastAndroid, Platform} from "react-native"; | ||
import {useEffect,useState} from "react"; | ||
@@ -15,33 +15,36 @@ | ||
export function useExitAppOnDoublePress(options) { | ||
let condition = true; | ||
let message = "Press Again To Exit"; | ||
let timeout = 2000; | ||
if(options){ | ||
let condition = options.condition || true; | ||
let message = options.message || "Press Again To Exit"; | ||
let timeout = options.timeout || 2000; | ||
} | ||
const[backPressed,setBackClickCount] = useState(0); | ||
const message = options && options.message? options.message : "Press Again To Exit"; | ||
const timeout = options && options.timeout? options.timeout : 2000; | ||
const condition = options && options.condition ? options.condition : false; | ||
useEffect(() => { | ||
BackHandler.addEventListener('hardwareBackPress', handleBackButton); | ||
if(options.condition && Platform.OS === 'android' ){ | ||
BackHandler.addEventListener('hardwareBackPress', handleBackButton); | ||
} | ||
return ()=>{ | ||
if (Platform.OS === 'android'){ | ||
BackHandler.removeEventListener('hardwareBackPress', handleBackButton); | ||
} | ||
} | ||
}, [condition, backPressed]); | ||
const [timeoutObj, setTimeoutObj] = useState(); | ||
}, [backPressed]); | ||
useEffect(()=>{ | ||
return ()=>{ | ||
clearTimeout(timeoutObj); | ||
} | ||
}); | ||
function handleBackButton() { | ||
if(condition){ | ||
if (backPressed > 0) { | ||
BackHandler.exitApp(); | ||
setBackClickCount(0); | ||
} else { | ||
setBackClickCount(backPressed + 1); | ||
ToastAndroid.show(message, ToastAndroid.SHORT); | ||
setTimeout(() => { | ||
setBackClickCount(0) | ||
}, timeout); | ||
return true; | ||
} | ||
if (backPressed > 0) { | ||
BackHandler.exitApp(); | ||
setBackClickCount(0); | ||
} else { | ||
setBackClickCount(backPressed + 1); | ||
ToastAndroid.show(message, ToastAndroid.SHORT); | ||
setTimeoutObj(setTimeout(() => { | ||
setBackClickCount(0) | ||
}, timeout)); | ||
return true; | ||
} | ||
else | ||
return false | ||
} | ||
} |
{ | ||
"name": "@shankarmorwal/react-native-exit-on-double-press", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "Custom hook that can be used to exit the app on double press. It uses small toaster inside. This will work only on android", | ||
@@ -5,0 +5,0 @@ "author": "Shankar Morwal <shankar@habilelabs.io", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
3268
46