Skip to content

Commit 718a3ed

Browse files
author
xuyuanxiang@home
committed
fix: 修复同步history和navigation的问题
1 parent 87a8fb2 commit 718a3ed

File tree

1 file changed

+17
-23
lines changed

1 file changed

+17
-23
lines changed

packages/umi-renderer-react-navigation/src/createHistoryNavigator.tsx

+17-23
Original file line numberDiff line numberDiff line change
@@ -71,20 +71,10 @@ function HistoryNavigator({
7171
[navigation, state.index, state.key],
7272
);
7373

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(
74+
React.useEffect(
75+
() =>
8776
history.listen((location: Location<any>, action: Action): void => {
77+
const state = navigation.dangerouslyGetState();
8878
if (state.routeNames.includes(location.pathname)) {
8979
switch (action) {
9080
case 'POP':
@@ -105,17 +95,21 @@ function HistoryNavigator({
10595
}
10696
}
10797
}),
108-
);
109-
return () => {
110-
for (const fn of subscribes) {
111-
if (typeof fn === 'function') {
112-
try {
113-
fn();
114-
} catch (ignored) {}
115-
}
98+
[navigation, history],
99+
);
100+
101+
React.useEffect(() => {
102+
if (history.index > state.index) {
103+
history.go(state.index - history.index);
104+
} else if (history.index < state.index) {
105+
const route = state.routes[state.index];
106+
if (route) {
107+
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore
108+
// @ts-ignore
109+
history.push(route.params ? `${route.name}?${stringify(route.params)}` : route.name);
116110
}
117-
};
118-
}, [navigation, history, state]);
111+
}
112+
}, [state.index, history]);
119113

120114
return <StackView {...rest} descriptors={descriptors} state={state} navigation={navigation} />;
121115
}

0 commit comments

Comments
 (0)