Skip to content

Instantly share code, notes, and snippets.

@alanhchoi
Last active September 15, 2022 17:57
Show Gist options
  • Save alanhchoi/e785757ba6521a32b6dd609031ecc219 to your computer and use it in GitHub Desktop.
Save alanhchoi/e785757ba6521a32b6dd609031ecc219 to your computer and use it in GitHub Desktop.
import { useCallback } from 'react';
import { useNavigation, NavigationProp, useFocusEffect } from '@react-navigation/native';
function delay(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
function ScreenA() {
const navigation = useNavigation<NavigationProp<any>>();
useFocusEffect(
useCallback(() => {
const timeoutId = setTimeout(() => {
// Schedule a navigation 5 seconds after this screen is focused.
navigation.navigate('ScreenB');
}, 5000);
return () => {
// If user leaves the screen, cancel the scheduled navigation.
clearTimeout(timeoutId);
};
}, [navigation])
);
return <></>;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment