Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable back button in react navigation

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 option, returning to the Login screen. Does anyone know how it can be hidden on the screen after the login screen? BTW, I'm also hiding it in the login screen by using:

const MainStack = StackNavigator({   Login: {     screen: Login,     navigationOptions: {       title: "Login",       header: {         visible: false,       },     },   },   // ... other screens here }) 
like image 605
EyalS Avatar asked Mar 16 '17 10:03

EyalS


People also ask

How do I customize the back button in React navigation?

The back button is fully customizable with headerLeft , but if you just want to change the title or image, there are other options for that — headerBackTitle , headerBackTitleStyle , and headerBackImageSource . You can use a callback for the options prop to access navigation and route objects.

How do you enable or disable a button in React?

Use the disabled prop to disable a button in React, e.g. <button disabled={true}>Click</button> . You can use the prop to conditionally disable the button based on the value of an input field or another variable or to prevent multiple clicks to the button. Copied!


2 Answers

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

v2-v4:

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

v5:

{          navigationOptions:  {     title: 'MyScreen',     headerLeft: ()=> null, // Note: using just `null` instead of a function should also work but could trigger a TS error } 

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.

Additional sources: thank you to the users that added comments below and helped keeping this answer updated for v5.

like image 174
Florin Dobre Avatar answered Oct 06 '22 07:10

Florin Dobre


Have you considered using this.props.navigation.replace( "HomeScreen" ) instead of this.props.navigation.navigate( "HomeScreen" ).

This way you are not adding anything to the stack. so HomeScreen won't wave anything to go back to if back button pressed in Android or screen swiped to the right in IOS.

More informations check the Documentation. And of course you can hide the back button by setting headerLeft: null in navigationOptions

like image 44
Tarik Chakur Avatar answered Oct 06 '22 06:10

Tarik Chakur