Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle back button behavior in bottom tabs in React Native?

I have a @react-navigation/bottom-tabs navigator when my app opens whose contents are like:

    <Tab.Navigator
        tabBarOptions={{
            activeTintColor: '#77dd77',
            inactiveTintColor: 'gray',
        }}
        tabBar={props => <MyTabBar {...props} />}
        backBehavior={"history"}
        >
        <Tab.Screen
            name="Home"
            component={Home}
            options={{ title: 'Home' }}
        />
        <Tab.Screen
            name="Orders"
            component={Orders}
            options={{ title: 'Orders' }}
        />
        <Tab.Screen
            name="Profile"
            component={Profile}
            options={{ title: 'Profile' }}
        />
    </Tab.Navigator>

I have a BackHandler in my code that makes the app exit when back button is pressed from the home page. Everything is fine and I have checked that the backhandler gets called when back button is pressed.

But when I switch to any other tab and then return to the homepage and press back to exit the app, backhandler stops working and the app shows error "The action 'GO_BACK'was not handled by any navigator. Is there any screen to go back to?"

This is a development-only warning but in the signed version, the app doesn't show any error and doesn't even exit.

How can I address this 'GO_BACK' action?

like image 360
szskdgi Avatar asked Apr 17 '20 09:04

szskdgi


People also ask

How do you handle back button press in react native?

To handle the Android Back Button Press in the React Native we have to register the hardwareBackPress event listener with a callback function, which will be called after pressing the Back Button.

How do I customize the bottom tab in react native?

To add icons to each tab, first import the Icon component from react-native-vector-icons library inside the navigation/TabNavigator/index. js file. For this example, let's use AntDesign based icons. // after other import statements import Icon from 'react-native-vector-icons/AntDesign';

How do I hide the back button in react native navigation?

1) To make the back button disappear in react-navigation v2 or newer: Have you considered using this. We need to set false to the gesturesEnabled along with headerLeft to null . You can hide the back button using left:null , but for android devices it's still able to go back when the user presses the back button.


1 Answers

I was facing a similar issue, just find out the solution.

The problem was that I was trying to handle backHandler from the screen itself, but it just doesn't work like that with tab navigator (and maybe with react-navigation as a whole? I dunno).

Any way, you just need to add a listener for 'focus' (~componentDidMount) and 'blur' (~componentWillUnmount) like this:

        <Tab.Screen name="Home" component={HomeScreen} 
          listeners={{ focus: () => BackHandler.addEventListener('hardwareBackPress',handleBackButton)
                      ,blur: () => BackHandler.removeEventListener('hardwareBackPress',handleBackButton)
          }}
        />

Where:

function handleBackButton  ()  {
  BackHandler.exitApp();
  return true;
}

Now the backHandler function works in this way only in HomeScreen, and as usual in other screens.

like image 115
YosefBro Avatar answered Oct 20 '22 19:10

YosefBro