-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathroot.js
More file actions
49 lines (43 loc) · 1.72 KB
/
root.js
File metadata and controls
49 lines (43 loc) · 1.72 KB
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
import React, {useEffect} from 'react';
import {NavigationContainer} from '@react-navigation/native';
import {createNativeStackNavigator} from '@react-navigation/native-stack';
import SiftReactNative from 'sift-react-native';
import ScreenTwo from './screens/screentwo';
import ScreenOne from './screens/screenone';
const Stack = createNativeStackNavigator();
const Root = () => {
const routeNameRef = React.useRef();
const navigationRef = React.useRef();
useEffect(() => {
const initialRouteName = routeNameRef.current;
console.log('Initial screen is:', initialRouteName);
SiftReactNative.setPageName(`screen_${initialRouteName}`);
SiftReactNative.upload();
}, []);
return (
<NavigationContainer
ref={navigationRef}
onReady={() =>
(routeNameRef.current = navigationRef.current.getCurrentRoute().name)
}
onStateChange={() => {
const previousRouteName = routeNameRef.current;
const currentRouteName = navigationRef.current.getCurrentRoute().name;
if (previousRouteName !== currentRouteName) {
// Replace the line below to add the tracker from a mobile analytics SDK
//alert(`The route changed to ${currentRouteName}`);
console.log('Screen focused is:', currentRouteName);
SiftReactNative.setPageName(`screen_${currentRouteName}`);
SiftReactNative.upload();
}
// Save the current route name for later comparison
routeNameRef.current = currentRouteName;
}}>
<Stack.Navigator>
<Stack.Screen name="ScreenOne" component={ScreenOne} />
<Stack.Screen name="ScreenTwo" component={ScreenTwo} />
</Stack.Navigator>
</NavigationContainer>
);
};
export default Root;