以下代码似乎有效。但是,对于这样一个基本(我认为)要求,这是一个相当长的代码。我想知道是否有更简单的解决方案。
import { CommonActions } from "@react-navigation/native";
props.navigation.dispatch(state => {
// 1. Find (by name) the stack, which needs to be reset
let i;
for (i = 0; i < state.routes.length; i++) {
if (state.routes[i].name === "MyStack") {
break;
}
}
// 2. If there has been any activity for the stack, which we found above, we reset it
let routes2 = { ...state.routes };
if (routes2[i].state) {
delete routes2[i].state;
}
// 3. Update state
return CommonActions.reset({
...state,
routes2,
});
});
// 4. navigate to the stack (after it has been reset)
props.navigation.navigate("MyStack");
要重置抽屉堆栈:
import { CommonActions } from "@react-navigation/native";
// ...........
<MyNav.Navigator
screenListeners={({ navigation, route }) => ({
focus: (e) => {
navigation.dispatch(state => {
// 1. Find the stack, which needs to be reset -> Get "i"
let i;
for (i = 0; i < state.routes.length; i++) {
if (state.routes[i].name === route.name) {
break;
}
}
// 2. If there has been any activity for the stack, which we found above (having "i"), we reset it
let routes2 = { ...state.routes };
if (routes2[i].state) {
delete routes2[i].state;
}
// 3. Update the "state"
return CommonActions.reset({
...state,
routes2,
});
});
// 4. navigate to the stack (after it has been reset)
navigation.navigate(route.name);
}
})}
>
// ...........
</MyNav.Navigator>