Last active
April 2, 2021 07:25
-
-
Save kaushal9678/74783476be06fe7ef8196c39b63589fd to your computer and use it in GitHub Desktop.
A file where I handle all incoming DeepLink url's
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 DeepLinking from 'react-native-deep-linking'; | |
import { ColorSchemeName, Linking } from "react-native"; | |
import { manageRoutes } from "./DeepLinkNavigation"; | |
export default function Navigation({ | |
colorScheme, | |
}: { | |
colorScheme: ColorSchemeName; | |
}) { | |
const navigationRef = useRef(); | |
const routeNameRef = useRef(); | |
const handleUrl = ({ url }: { url: string }) => { | |
Linking.canOpenURL(url).then((supported) => { | |
if (supported) { | |
DeepLinking.evaluateUrl(url); | |
manageRoutes(url) | |
} | |
}); | |
}; | |
useEffect(() => { | |
//Note: If you don't want to use DeepLinking library you can also use react-native listener as per doc. | |
DeepLinking.addScheme('deeplink://'); //In Deeplinking you have to add your scheme that | |
//you defined in LinkingConfiguration | |
Linking.addEventListener('url', handleUrl); | |
return () => { | |
Linking.removeEventListener('url', handleUrl); | |
}; | |
}, []); | |
return ( | |
<SafeAreaProvider> | |
<NavigationContainer | |
uriPrefix={linking.prefixes} | |
linking={LinkingConfiguration} | |
theme={colorScheme === "dark" ? ThemeDark : ThemeLight} | |
ref={navigationRef} | |
onReady={() => | |
(routeNameRef.current = navigationRef.current.getCurrentRoute().name) | |
} | |
onStateChange={async () => { | |
const previousRouteName = routeNameRef.current; | |
const currentRouteName = navigationRef?.current?.getCurrentRoute().name; | |
if (previousRouteName !== currentRouteName) {// For Firebase Analytics | |
} | |
// Save the current route name for later comparison | |
routeNameRef.current = currentRouteName; | |
}} | |
> | |
<RootNavigator /> | |
</NavigationContainer> | |
</SafeAreaProvider> | |
); | |
} | |
// A root stack navigator is often used for displaying modals on top of all other content | |
// Read more here: https://reactnavigation.org/docs/modal | |
const Stack = createStackNavigator()//<RootStackParamList>(); | |
function RootNavigator() { | |
return ( | |
<Stack.Navigator screenOptions={{ headerShown: false }}> | |
<Stack.Screen name="BottomTabs" component={BottomTabNavigator} /> | |
{/* <Stack.Screen | |
name="NotFound" | |
component={NotFoundScreen} | |
options={{title: 'Oops!'}} | |
/> */} | |
</Stack.Navigator> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment