Remove screen from stack navigator

后端 未结 2 695
野性不改
野性不改 2021-01-02 07:56

I’ve observed Back button logic works seeing the sequence of screens in the stack. I’ve a Drawer navigator placed inside stack navigator.

On top of the stack I’ve S

相关标签:
2条回答
  • 2021-01-02 08:05

    One solution would be to reset the stack inside the splash screen component and redirect the user to the screen that you prefer:

    import { NavigationActions } from 'react-navigation'
    
    const resetAction = NavigationActions.reset({
      index: 0,
      actions: [
        NavigationActions.navigate({ routeName: 'Drawer'})
      ]
    })
    this.props.navigation.dispatch(resetAction)
    

    For newer versions of react-navigation :

    import { StackActions, NavigationActions } from 'react-navigation';
    
    const resetAction = StackActions.reset({
      index: 0,
      actions: [NavigationActions.navigate({ routeName: 'Profile' })],
    });
    this.props.navigation.dispatch(resetAction);
    

    Link to the official documentation

    0 讨论(0)
  • 2021-01-02 08:05

    I solved it using this

    code:

    const prevGetStateForActionHomeStack = HomeStack.router.getStateForAction;
    HomeStack.router = {
      ...HomeStack.router,
      getStateForAction(action, state) {
        if (state && action.type === 'ReplaceCurrentScreen') {
          const routes = state.routes.slice(0, state.routes.length - 1);
          routes.push(action);
          return {
            ...state,
            routes,
            index: routes.length - 1,
          };
        }
        return prevGetStateForActionHomeStack(action, state);
      },
    };
    replaceScreen = () => {
      const { locations, position } = this.props.navigation.state.params;
      this.props.navigation.dispatch({
        key: 'NearMeMap',
        type: 'ReplaceCurrentScreen',
        routeName: 'NearMeMap',
        params: { locations, position },
      });
    };
    

    Also if you need in-depth explanation of the code, watch this short video here

    0 讨论(0)
提交回复
热议问题