【问题标题】:React navigation "replace" the previous 2 screens instead of only 1反应导航“替换”前 2 个屏幕,而不是只有 1 个
【发布时间】:2020-06-05 10:37:31
【问题描述】:
在反应导航中,如何替换堆栈中的最后两个屏幕?
例如,如果我当前的堆栈是screen1 -> screen2 -> screen3,当我在screen3上调用.replace('screen4')时,
它变成screen1 -> screen2 -> screen4
但我想要一种方法,让它变成screen1 -> screen4
【问题讨论】:
标签:
react-native
react-navigation
react-navigation-v5
【解决方案1】:
您需要使用reset 来执行此操作:
import { CommonActions } from "@react-navigation/native";
// ...
navigation.dispatch(state => {
// Remove the last 2 routes from current list of routes
const routes = state.routes.slice(0, -2);
// Reset the state to the new state with updated list of routes
return CommonActions.reset({
...state,
index: routes.length - 1,
routes
});
});