Skip to content

Instantly share code, notes, and snippets.

@itispx
Created October 14, 2022 06:13
Show Gist options
  • Save itispx/3da47bdc8d3a775af9fc3a494638befb to your computer and use it in GitHub Desktop.
Save itispx/3da47bdc8d3a775af9fc3a494638befb to your computer and use it in GitHub Desktop.
useBackAction hook
import { useEffect } from "react";
import { BackHandler } from "react-native";
const useBackAction = (func, dependencies) => {
useEffect(() => {
const backAction = () => {
func();
return true;
};
BackHandler.addEventListener("hardwareBackPress", backAction);
return () =>
BackHandler.removeEventListener("hardwareBackPress", backAction);
}, dependencies);
};
export default useBackAction;
8 lines (14 sloc) 459 Bytes
import { useEffect } from "react";
import { BackHandler } from "react-native";
const useBackAction = (func: () => void, dependencies: [] | undefined) => {
useEffect(() => {
const backAction = () => {
func();
return true;
};
BackHandler.addEventListener("hardwareBackPress", backAction);
return () =>
BackHandler.removeEventListener("hardwareBackPress", backAction);
}, dependencies);
};
export default useBackAction;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment