-
Notifications
You must be signed in to change notification settings - Fork 34
/
NavigationContainer.tsx
58 lines (52 loc) · 1.45 KB
/
NavigationContainer.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import {
NavigationContainer as ReactNavigationContainer,
type NavigationContainerRef,
type NavigationState,
type ParamListBase,
} from '@react-navigation/native';
import React, { useRef } from 'react';
import { type RootNavigatorParamList } from '@/navigation/Root';
function formatBreadcrumbs<State extends NavigationState<ParamListBase>>(
name: string,
state: State | undefined,
params?: unknown
): string {
if (state) {
const route = state.routes[state.index];
return `${name}/${formatBreadcrumbs(
route.name,
route.state,
route.params
)}`;
} else {
return `${name}${params ? `(${JSON.stringify(params)})` : ''}`;
}
}
export default function NavigationContainer({
children,
}: {
children: React.ReactNode;
}) {
// Used to log navigation events
const navigationRef =
useRef<NavigationContainerRef<RootNavigatorParamList>>(null);
const routePath = useRef<string>();
return (
<ReactNavigationContainer
ref={navigationRef}
onReady={() => {
routePath.current = formatBreadcrumbs(
'Root',
navigationRef?.current?.getRootState()
);
}}
onStateChange={(state) => {
const previousRoute = routePath.current;
const currentRoute = formatBreadcrumbs('Root', state);
console.log(`${previousRoute} -> ${currentRoute}`);
routePath.current = currentRoute;
}}>
{children}
</ReactNavigationContainer>
);
}