gpt4 book ai didi

javascript - 有没有办法在react-navigation(react-native)中保存和恢复导航历史记录

转载 作者:行者123 更新时间:2023-12-03 14:32:21 24 4
gpt4 key购买 nike

我有一个带有 5 个屏幕的 StackNavigator(1)。在第三个屏幕之后,我导航到另一个 StackNavigator(2) 屏幕,在第二个 StackNavigator(2) 屏幕上进行一些操作后,我需要返回到 StackNavigator(1) 第四个屏幕,并且应该能够返回 StackNavigator 中的历史记录( 1)使用navigation.goBack()

我能够返回到 Stack StackNavigator(1) 第 4 个屏幕,但是当我使用 navigation.goBack() 时,它不会返回到 StackNavigator(1) 第 3 个屏幕

我需要在 StackNavigator(1) 中保存和恢复导航历史记录。

请告诉我是否有任何方法可以实现这一目标。

enter image description here

最佳答案

使用StackNavigator.router.getStateForAction来操作您的路由状态。

    //assume your StackNavigator1 just like below
const StackNavigator1 = new StackNavigator({
Screen1: {screen: Screen1},
Screen2: {screen: Screen2},
Screen3: {screen: Screen3},
Screen4: {screen: Screen4},
Screen5: {screen: Screen5}
});

const defaultGetStateForAction = StackNavigator1.router.getStateForAction;
//store the state when navigate to screen3
let state3;

StackNavigator1.router.getStateForAction = (action, state) => {

let newState = defaultGetStateForAction(action, state);
//print out the state object
console.log(`newState: ${newState}`);

//change all the routeName 'Screen3', 'Screen4' to match your own project
if (action.type === 'Navigation/NAVIGATE' && action.routeName === 'Screen3') {
//store the state of screen3 for restore it later
state3 = newState;
}

if (action.type === 'Navigation/NAVIGATE' && action.routeName === 'Screen4') {
//check if screen3 exist in the state, change the condition for your own need
let result = newState.routes.find((route) => {
return route.routeName === 'Screen3'
});
//if 'screen3' doesn't exist, add it to the route manually
if (result === undefined && state3 !== undefined) {
let route4 = newState.routes[newState.index];
let routes = state3.routes.slice();
routes.push(route4);
return {index: 3, routes}
}
}

return defaultGetStateForAction(action, state)
};

//... the rest code

关于javascript - 有没有办法在react-navigation(react-native)中保存和恢复导航历史记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45712926/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com