I am using React Navigation 5 in a project, and I'm having trouble trying to block a user from navigating back after a certain point.
The app uses a nested navigation structure similar to this:
ROOT (STACK)
|-- LoginScreens (STACK - options={{ gestureEnabled: false }} )
| |-- Login (SCREEN) -> when successful navigate to "Home"
| +-- Register (SCREEN) -> after registration, navigate to "Login"
|
+-- Home (TABS - options={{ gestureEnabled: false }} )
|-- BlahBlah (SCREEN)
|-- MyProfile (SCREEN)
+-- Dashboard (TABS)
|-- AllTasks (SCREEN)
+-- SomethingElse (SCREEN)
After a successful user login, the user is sent to the Home
screen and should not be able to navigate back to the LoginScreens
screen.
I have tried to use the componentDidMount
lifecycle method on Home
, as well as the useFocusEffect
hook, with the following:
- Placing a callback to React Native's
BackHandler
, returning true from the handler works (true means back action has been handled, no further back handlers will be called), but it will also block any back navigation within the screens inHome
(e.g. I cannot navigate back from Dashboard to MyProfile). - Using
navigation.reset({ index: 1, routes: [{ name: "Home" }] })
. Withoutindex: 1
the navigation just goes back to ROOT's initialRoute (in this case,LoginScreens
). Withindex: 1
, aMaximum update depth exceeded
error is thrown. - Instead navigating directly to
Home
, I have tried using anavigation.reset()
(note: no params, clears the entire navigation history), and after that navigate to theHome
screen. This doesn't achieve the desired effect since the current route (ROOT's initialRoute, in this case:LoginScreens
) is still pushed on the navigation history before navigating toHome
. - Combining navigation and reset calls in different ways, I have only managed to get JS angry and throw errors and exceptions at me.
Aaaaand... I have ran out of ideas. Does anyone have any suggestions ?