Skip to content

Commit

Permalink
fix: 修复同步history和navigation的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
xuyuanxiang@home committed Jun 16, 2020
1 parent 87a8fb2 commit 718a3ed
Showing 1 changed file with 17 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,10 @@ function HistoryNavigator({
[navigation, state.index, state.key],
);

React.useEffect(() => {
if (history.index > state.index) {
history.go(state.index - history.index);
} else if (history.index < state.index) {
const route = state.routes[state.index];
if (route) {
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore
// @ts-ignore
history.push(route.params ? `${route.name}?${stringify(route.params)}` : route.name);
}
}
const subscribes: (() => void)[] = [];
subscribes.push(
React.useEffect(
() =>
history.listen((location: Location<any>, action: Action): void => {
const state = navigation.dangerouslyGetState();
if (state.routeNames.includes(location.pathname)) {
switch (action) {
case 'POP':
Expand All @@ -105,17 +95,21 @@ function HistoryNavigator({
}
}
}),
);
return () => {
for (const fn of subscribes) {
if (typeof fn === 'function') {
try {
fn();
} catch (ignored) {}
}
[navigation, history],
);

React.useEffect(() => {
if (history.index > state.index) {
history.go(state.index - history.index);
} else if (history.index < state.index) {
const route = state.routes[state.index];
if (route) {
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore
// @ts-ignore
history.push(route.params ? `${route.name}?${stringify(route.params)}` : route.name);
}
};
}, [navigation, history, state]);
}
}, [state.index, history]);

return <StackView {...rest} descriptors={descriptors} state={state} navigation={navigation} />;
}
Expand Down

0 comments on commit 718a3ed

Please sign in to comment.