Back Button
Wed Dec 14 2022 06:58:06 GMT+0000 (Coordinated Universal Time)
Saved by
@skfaizan2301
#reactnative
#javascript
import React, { useEffect } from "react";
import { Text, View, StyleSheet, BackHandler, Alert } from "react-native";
import { useBackHandler } from "@react-native-community/hooks"
const App = () => {
// Callback function for back action
const backActionHandler = () => {
Alert.alert("Alert!", "Are you sure you want to exit app?", [
{
text: "Cancel",
onPress: () => null,
style: "cancel"
},
{ text: "YES", onPress: () => BackHandler.exitApp() }
]);
return true;
};
// hook which handles event listeners under the hood
useBackHandler(backActionHandler)
return (
<View style={styles.container}>
<Text style={styles.text}>Click Back button!</Text>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: "center",
justifyContent: "center"
},
text: {
fontSize: 18,
fontWeight: "bold"
}
});
export default App;
content_copyCOPY
Back Button in react native
Comments