react-navigation goBack返回到指定页面

const AppNavigator = StackNavigator({
    Home: {screen: Home},
    ShopDetail: {screen: ShopDetail}
});

const defaultGetStateForAction = AppNavigator.router.getStateForAction;
AppNavigator.router.getStateForAction = (action, state) => {
    // goBack返回指定页面
    if (state && action.type === 'Navigation/BACK' && action.key) {
        const backRoute = state.routes.find((route) => route.routeName === action.key);
        if (backRoute) {
            const backRouteIndex = state.routes.indexOf(backRoute);
            const purposeState = {
                ...state,
                routes: state.routes.slice(0, backRouteIndex + 1),
                index: backRouteIndex,
            };
            return purposeState;
        }
    }
    return defaultGetStateForAction(action, state)
};
this.props.navigation.goBack('Home') 返回页面的指定路由

你可能感兴趣的:(Android)