React Navigation,如何返回特定屏幕?

Joh*_*nyQ 4 javascript react-native react-navigation expo

我正在使用react-navigation:^1.0.0-beta.9有没有办法回到特定的屏幕(不是前一个到当前)?我也整合了我react-navigation的redux.我查看了文档并看到back接受了一个key参数然而这对我来说并不清楚,并尝试将其放在屏幕的名称中,back({ key: 'Screen B' })但它不起作用可能因为它期望key随机生成哪个除非特别指定.

例如我有这个 StackNavigator

Screen A
- Screen B
  - Screen C
    - Screen D (Current)
Run Code Online (Sandbox Code Playgroud)

假设我现在正在使用Screen D,我想回到Screen B如何实现它?

我不想使用,navigation.navigate('Screen B')因为它会在堆栈中添加另一个屏幕,而不是我期待的.

ufx*_*eng 14

1.Inside screen D派出一些行动:

    const {navigation} = this.props;
    navigation.dispatch({
        routeName: 'B',
        type: 'GoToRoute',
    });
Run Code Online (Sandbox Code Playgroud)

2.在MyStackNavigator.js中处理此操作

const MyStackNavigator = new StackNavigator(//...);
const defaultGetStateForAction = MyStackNavigator.router.getStateForAction;
MyStackNavigator.router.getStateForAction = (action, state) => {            
    if (state && action.type === 'GoToRoute') {           
        let index = state.routes.findIndex((item) => {
            return item.routeName === action.routeName
        });
        const routes = state.routes.slice(0, index+1);
        return {
            routes,
            index
        };    
    }       
    return defaultGetStateForAction(action, state);
};
Run Code Online (Sandbox Code Playgroud)

然后你可以从去screen D到screen B直接