Disable back button in react navigation

前端 未结 18 1606
长发绾君心
长发绾君心 2020-12-12 18:31

I\'m using react native navigation (react-navigation) StackNavigator. it starts from the Login page throughout the whole lifecycle of the app. I don\'t want to have a back

相关标签:
18条回答
  • 2020-12-12 19:07

    Simply doing

    headerLeft: null
    

    might be deprecated by the time you read this answer. You should use following

       navigationOptions = {
            headerTitle : "Title",
            headerLeft : () => {},
        }
    
    0 讨论(0)
  • 2020-12-12 19:09

    1) To make the back button disappear in react-navigation v2 or newer:

    navigationOptions:  {
        title: 'MyScreen',
        headerLeft: null
    }
    

    2) If you want to clean navigation stack:

    Assuming you are on the screen from which you want to navigate from:

    If you are using react-navigation version v5 or newer you can use navigation.reset or CommonActions.reset:

     // Replace current navigation state with a new one,
     // index value will be the current active route:
    
    navigation.reset({
      index: 0,
      routes: [{ name: 'Profile' }],
    });
    

    Source and more info here: https://reactnavigation.org/docs/navigation-prop/#reset

    Or:

    navigation.dispatch(
      CommonActions.reset({
        index: 1,
        routes: [
          { name: 'Home' },
          {
            name: 'Profile',
            params: { user: 'jane' },
          },
        ],
      })
    );
    

    Source and more info here: https://reactnavigation.org/docs/navigation-actions/#reset

    For older versions of react-navigation:

    v2-v4 use StackActions.reset(...)

    import { StackActions, NavigationActions } from 'react-navigation';
    
    const resetAction = StackActions.reset({
      index: 0, // <-- currect active route from actions array
      actions: [
        NavigationActions.navigate({ routeName: 'myRouteWithDisabledBackFunctionality' }),
      ],
    });
    
    this.props.navigation.dispatch(resetAction);
    

    v1 use NavigationActions.reset

    3) For android you will also have to disable the hardware back button using the BackHandler:

    http://reactnative.dev/docs/backhandler.html

    or if you want to use hooks:

    https://github.com/react-native-community/hooks#usebackhandler

    otherwise the app will close at android hardware back button press if navigation stack is empty.

    0 讨论(0)
  • 2020-12-12 19:13

    We need to set false to the gesturesEnabled along with headerLeft to null. Because we can navigate back by swiping the screen as well.

    navigationOptions:  {
       title: 'Title',
       headerLeft: null,
       gesturesEnabled: false,
    }
    
    0 讨论(0)
  • 2020-12-12 19:17

    found it myself ;) adding:

      left: null,
    

    disable the default back button.

    const MainStack = StackNavigator({
      Login: {
        screen: Login,
        navigationOptions: {
          title: "Login",
          header: {
            visible: false,
          },
        },
      },
      FirstPage: {
        screen: FirstPage,
        navigationOptions: {
          title: "FirstPage",
          header: {
            left: null,
          }
        },
      },
    
    0 讨论(0)
  • 2020-12-12 19:17

    We can fix it by setting headerLeft to null

    static navigationOptions =({navigation}) => {
        return {
            title: 'Rechercher une ville',
            headerLeft: null,
        }  
    }
    
    0 讨论(0)
  • 2020-12-12 19:17
    headerLeft: null
    

    This won't work in the latest react native version

    It should be:

    navigationOptions = {
     headerLeft:()=>{},
    }
    

    For Typescript:

    navigationOptions = {
     headerLeft:()=>{return null},
    }
    
    0 讨论(0)
提交回复
热议问题