Search code examples
react-nativenavigationreact-native-navigation

Tab Navigator's `goBack` not working properly on React Native Navigation


I created a tab navigator for an app using @react-navigation/native: ^6.0.16. But when I tried to go back it always goes back to the first screen on the tab navigator. For example if I navigate from Home >> Notifications >> Profile and press the android back button instead of Notifications I reach Home. Basically going back on any tab (other than Home) navigates me back to home. This issue does not seem to be a problem for @react-navigation/native: v5.x but I rather not go back to using version 5.x. Is there any way I can fix this issue on version 6.x?

You can checkout a small snack of the issue here. Just goto package.json and replace "@react-navigation/native": "^6.0.16", with "@react-navigation/native": "^5.9.8", to get the intended output.


Solution

  • As per docs here, you can specify the behaviour you want:

    This controls what happens when goBack is called in the navigator. This includes pressing the device's back button or back gesture on Android.

    It supports the following values:

    firstRoute - return to the first screen defined in the navigator (default) initialRoute - return to initial screen passed in initialRouteName prop, if not passed, defaults to the first screen order - return to screen defined before the focused screen history - return to last visited screen in the navigator; if the same screen is visited multiple times, the older entries are dropped from the history none - do not handle back button