Created
October 14, 2022 06:13
-
-
Save itispx/3da47bdc8d3a775af9fc3a494638befb to your computer and use it in GitHub Desktop.
useBackAction hook
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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