mirror of
https://github.com/bulwarkmail/native.git
synced 2026-11-01 16:05:39 +00:00
24 lines
853 B
TypeScript
24 lines
853 B
TypeScript
import { useCallback } from 'react';
|
|
import { BackHandler } from 'react-native';
|
|
import { useFocusEffect } from '@react-navigation/native';
|
|
|
|
/**
|
|
* While `active` and the screen is focused, Android's hardware back runs
|
|
* `onBack` instead of navigating (e.g. to leave a selection mode first).
|
|
* Tab screens stay mounted while another tab is shown, so the listener is
|
|
* tied to focus: a plain BackHandler listener would also swallow the back
|
|
* presses meant for the other tabs.
|
|
*/
|
|
export function useBackWhileFocused(active: boolean, onBack: () => void): void {
|
|
useFocusEffect(
|
|
useCallback(() => {
|
|
if (!active) return undefined;
|
|
const subscription = BackHandler.addEventListener('hardwareBackPress', () => {
|
|
onBack();
|
|
return true;
|
|
});
|
|
return () => subscription.remove();
|
|
}, [active, onBack]),
|
|
);
|
|
}
|