mirror of
https://github.com/zhigang1992/react-navigation.git
synced 2026-02-13 17:47:32 +08:00
31 lines
661 B
TypeScript
31 lines
661 B
TypeScript
import * as React from 'react';
|
|
import { NavigationContainerRef } from '@react-navigation/core';
|
|
import { BackHandler } from 'react-native';
|
|
|
|
export default function useBackButton(
|
|
ref: React.RefObject<NavigationContainerRef>
|
|
) {
|
|
React.useEffect(() => {
|
|
const subscription = BackHandler.addEventListener(
|
|
'hardwareBackPress',
|
|
() => {
|
|
const navigation = ref.current;
|
|
|
|
if (navigation == null) {
|
|
return false;
|
|
}
|
|
|
|
if (navigation.canGoBack()) {
|
|
navigation.goBack();
|
|
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
);
|
|
|
|
return () => subscription.remove();
|
|
}, [ref]);
|
|
}
|