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:
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 in Home
(e.g. I cannot navigate back from Dashboard to MyProfile).navigation.reset({ index: 1, routes: [{ name: "Home" }] })
. Without index: 1
the navigation just goes back to ROOT's initialRoute (in this case, LoginScreens
). With index: 1
, a Maximum update depth exceeded
error is thrown.Home
, I have tried using a navigation.reset()
(note: no params, clears the entire navigation history), and after that navigate to the Home
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 to Home
.Aaaaand... I have ran out of ideas. Does anyone have any suggestions ?
Well, I have to admit, its was not easy to find the new reset method's syntax for v5, man... ReactNavigation docs really need an in site search functionality.
Anyway, reset method can be used, and worked perfectly for me.
It looks something like:
import { CommonActions } from '@react-navigation/native';
navigation.dispatch(
CommonActions.reset({
index: 0,
routes: [
{
name: 'Home',
params: { user: 'jane' },
},
],
})
);
I made a helper function which I am using in multiple places in my app, that looks like:
import { CommonActions } from '@react-navigation/native';
export const resetStackAndNavigate = (navigation, path) => {
navigation.dispatch(CommonActions.reset({ index: 0, routes: [{ name: path }] }));
};