React Navigation: Navigate Back To Root using NavigationActions.reset, goBack and getStateForAction

前端 未结 7 695
灰色年华
灰色年华 2020-12-28 15:05

Say I\'ve navigated through 4 screens in my StackNavigator App and now I want to go back to the first screen. There seems to be three different ways to do this and they do n

7条回答
  •  伪装坚强ぢ
    2020-12-28 15:56

    For 2020 / react-navigation-stack I use the following code when setting up the StackNavigator:

    import { createStackNavigator, CardStyleInterpolators } from 'react-navigation-stack';
    import { Easing } from 'react-native';
    
    const Navigator = createStackNavigator({
      // ...
    }, {
      defaultNavigationOptions: ({ navigation }) => {
        const animation = navigation.getParam('_animation');
        return {
          // ...
          cardStyleInterpolator: CardStyleInterpolators.forHorizontalIOS,
          ...(animation === 'back' && {
            gestureDirection: 'horizontal-inverted',
          }),
          ...(animation === 'skip' && {
            transitionSpec: {
              open: { animation: 'timing', config: { duration: 0, easing: Easing.step1 }, },
              close: { animation: 'timing', config: { duration: 0, easing: Easing.step0 }, },
            },
          }),
        };
      },
    });
    
    

    and the the _animation param to override the animation style

    // replace to a route not in the stack normally creates a forward animation, but
    // we want a backwards animation
    this.props.navigation.dispatch(
      StackActions.replace({ routeName: 'Login', params: { _animation: 'back' } })
    );
    

提交回复
热议问题