File tree Expand file tree Collapse file tree 3 files changed +42
-3
lines changed Expand file tree Collapse file tree 3 files changed +42
-3
lines changed Original file line number Diff line number Diff line change @@ -2,7 +2,7 @@ import React, { useEffect } from 'react';
2
2
import { StyleSheet } from 'react-native' ;
3
3
4
4
import { GestureHandlerRootView } from 'react-native-gesture-handler' ;
5
- import { NavigationContainer } from '@react-navigation/native' ;
5
+ import { NavigationContainer , useNavigationContainerRef } from '@react-navigation/native' ;
6
6
import Instabug , {
7
7
CrashReporting ,
8
8
InvocationEvent ,
@@ -20,6 +20,7 @@ import { QueryClient, QueryClientProvider } from 'react-query';
20
20
const queryClient = new QueryClient ( ) ;
21
21
22
22
export const App : React . FC = ( ) => {
23
+ const navigationRef = useNavigationContainerRef ( ) ;
23
24
useEffect ( ( ) => {
24
25
Instabug . init ( {
25
26
token : 'deb1910a7342814af4e4c9210c786f35' ,
@@ -33,11 +34,15 @@ export const App: React.FC = () => {
33
34
} ) ;
34
35
} , [ ] ) ;
35
36
37
+ useEffect ( ( ) => {
38
+ Instabug . setNavigationListener ( navigationRef ) ;
39
+ } , [ navigationRef ] ) ;
40
+
36
41
return (
37
42
< GestureHandlerRootView style = { styles . root } >
38
43
< NativeBaseProvider theme = { nativeBaseTheme } >
39
44
< QueryClientProvider client = { queryClient } >
40
- < NavigationContainer onStateChange = { Instabug . onStateChange } theme = { navigationTheme } >
45
+ < NavigationContainer theme = { navigationTheme } ref = { navigationRef } >
41
46
< RootTabNavigator />
42
47
</ NavigationContainer >
43
48
</ QueryClientProvider >
Original file line number Diff line number Diff line change 1
1
import type React from 'react' ;
2
2
import { Platform , findNodeHandle , processColor } from 'react-native' ;
3
3
4
- import type { NavigationState as NavigationStateV5 } from '@react-navigation/native' ;
4
+ import type {
5
+ NavigationContainerRefWithCurrent ,
6
+ NavigationState as NavigationStateV5 ,
7
+ } from '@react-navigation/native' ;
5
8
import type { ComponentDidAppearEvent } from 'react-native-navigation' ;
6
9
import type { NavigationAction , NavigationState as NavigationStateV4 } from 'react-navigation' ;
7
10
@@ -533,6 +536,19 @@ export const onStateChange = (state?: NavigationStateV5) => {
533
536
} , 1000 ) ;
534
537
} ;
535
538
539
+ /**
540
+ * Sets a listener for screen change
541
+ * @param navigationRef a refrence of a navigation container
542
+ *
543
+ */
544
+ export const setNavigationListener = (
545
+ navigationRef : NavigationContainerRefWithCurrent < ReactNavigation . RootParamList > ,
546
+ ) => {
547
+ navigationRef . addListener ( 'state' , ( ) => {
548
+ onStateChange ( navigationRef . getRootState ( ) ) ;
549
+ } ) ;
550
+ } ;
551
+
536
552
export const reportScreenChange = ( screenName : string ) => {
537
553
NativeInstabug . reportScreenChange ( screenName ) ;
538
554
} ;
Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ import '../mocks/mockInstabugUtils';
2
2
import '../mocks/mockNetworkLogger' ;
3
3
4
4
import { Platform , findNodeHandle , processColor } from 'react-native' ;
5
+ import type { NavigationContainerRefWithCurrent } from '@react-navigation/native' ; // Import the hook
5
6
6
7
import { mocked } from 'jest-mock' ;
7
8
import waitForExpect from 'wait-for-expect' ;
@@ -236,6 +237,23 @@ describe('Instabug Module', () => {
236
237
await waitForExpect ( ( ) => expect ( NativeInstabug . reportScreenChange ) . toBeCalledTimes ( 2 ) ) ;
237
238
} ) ;
238
239
240
+ it ( 'setNavigationListener should call the onStateChange on a screen change' , async ( ) => {
241
+ const mockNavigationContainerRef = {
242
+ current : null ,
243
+ navigate : jest . fn ( ) ,
244
+ reset : jest . fn ( ) ,
245
+ goBack : jest . fn ( ) ,
246
+ dispatch : jest . fn ( ) ,
247
+ getRootState : jest . fn ( ) ,
248
+ canGoBack : jest . fn ( ) ,
249
+ addListener : jest . fn ( ) ,
250
+ removeListener : jest . fn ( ) ,
251
+ } as unknown as NavigationContainerRefWithCurrent < ReactNavigation . RootParamList > ;
252
+
253
+ Instabug . setNavigationListener ( mockNavigationContainerRef ) ;
254
+ expect ( mockNavigationContainerRef . addListener ) . toBeCalledTimes ( 1 ) ;
255
+ } ) ;
256
+
239
257
it ( 'should call the native method init' , ( ) => {
240
258
const instabugConfig = {
241
259
token : 'some-token' ,
You can’t perform that action at this time.
0 commit comments