@@ -15,18 +15,16 @@ import {
15
15
StackNavigationConfig ,
16
16
StackNavigationOptions ,
17
17
} from '@react-navigation/stack/lib/typescript/src/types' ;
18
- import { History , Location , Action } from 'history' ;
19
- import { parse } from 'querystring' ;
18
+ import { Location , Action } from 'history' ;
19
+ import { parse , stringify } from 'querystring' ;
20
20
21
21
function HistoryNavigator ( {
22
22
initialRouteName,
23
23
children,
24
24
screenOptions,
25
25
history,
26
26
...rest
27
- } : DefaultNavigatorOptions < StackNavigationOptions > &
28
- StackRouterOptions &
29
- StackNavigationConfig & { history : History < any > } ) {
27
+ } : DefaultNavigatorOptions < StackNavigationOptions > & StackRouterOptions & StackNavigationConfig & { history : any } ) {
30
28
const defaultOptions = {
31
29
gestureEnabled : Platform . OS === 'ios' ,
32
30
animationEnabled : Platform . OS !== 'web' ,
@@ -73,27 +71,52 @@ function HistoryNavigator({
73
71
[ navigation , state . index , state . key ] ,
74
72
) ;
75
73
76
- React . useEffect (
77
- ( ) =>
74
+ React . useEffect ( ( ) => {
75
+ if ( history . index > state . index ) {
76
+ history . go ( state . index - history . index ) ;
77
+ } else if ( history . index < state . index ) {
78
+ const route = state . routes [ state . index ] ;
79
+ if ( route ) {
80
+ // eslint-disable-next-line @typescript-eslint/ban-ts-ignore
81
+ // @ts -ignore
82
+ history . push ( route . params ? `${ route . name } ?${ stringify ( route . params ) } ` : route . name ) ;
83
+ }
84
+ }
85
+ const subscribes : ( ( ) => void ) [ ] = [ ] ;
86
+ subscribes . push (
78
87
history . listen ( ( location : Location < any > , action : Action ) : void => {
79
88
if ( state . routeNames . includes ( location . pathname ) ) {
80
89
switch ( action ) {
81
90
case 'POP' :
82
- if ( navigation . canGoBack ( ) ) {
91
+ if ( state . index > history . index ) {
83
92
navigation . dispatch ( StackActions . pop ( ) ) ;
84
93
}
85
94
break ;
86
95
case 'PUSH' :
87
- navigation . dispatch ( StackActions . push ( location . pathname , parse ( location . search . replace ( '?' , '' ) ) ) ) ;
96
+ if ( state . index < history . index ) {
97
+ navigation . dispatch ( StackActions . push ( location . pathname , parse ( location . search . replace ( '?' , '' ) ) ) ) ;
98
+ }
88
99
break ;
89
100
case 'REPLACE' :
90
- navigation . dispatch ( StackActions . replace ( location . pathname , parse ( location . search . replace ( '?' , '' ) ) ) ) ;
101
+ if ( state . index === history . index ) {
102
+ navigation . dispatch ( StackActions . replace ( location . pathname , parse ( location . search . replace ( '?' , '' ) ) ) ) ;
103
+ }
91
104
break ;
92
105
}
93
106
}
94
107
} ) ,
95
- [ navigation , history ] ,
96
- ) ;
108
+ ) ;
109
+ return ( ) => {
110
+ for ( const fn of subscribes ) {
111
+ if ( typeof fn === 'function' ) {
112
+ try {
113
+ fn ( ) ;
114
+ } catch ( ignored ) { }
115
+ }
116
+ }
117
+ } ;
118
+ } , [ navigation , history , state ] ) ;
119
+
97
120
return < StackView { ...rest } descriptors = { descriptors } state = { state } navigation = { navigation } /> ;
98
121
}
99
122
0 commit comments