Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@shankarmorwal/react-native-exit-on-double-press

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@shankarmorwal/react-native-exit-on-double-press - npm Package Compare versions

Comparing version 1.0.1 to 1.0.2

55

lib/useExitAppOnDoublePress.js

@@ -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",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc