I am struggling with a situation. So I have three stack navigators.
navigator1 => screen1
navigator2 => screen2
navigator3 => screen3, screen4, screen5
I navigate from screen1
to screen2
. And then from screen2
to screen3
then screen4
then screen5
and then navigate to screen2
again. But now when I am on screen2
, onBack press I don't want to go on screen5
, instead want to go directly on screen1
.
When I tried to pop screen5
, screen4
and screen3
from stack just before navigating to screen2
from screen5
using the follwing code. It didn't work, still screen3
remains on stack.
import { StackActions } from '@react-navigation/native';
navigation.dispatch(
StackActions.popToTop()
);
OR
import { StackActions } from '@react-navigation/native';
navigation.dispatch(
StackActions.pop(3)
);
How to remove the navigator3
from the navigation stack? So that I would not go on screen5
from screen2
again onBackPress
I am using react-navigation version 5.x.