How to replace multiple routes to prevent user from going back? #880
-
I have 4 consecutive screens inside a stack navigator, A1 -> A2 -> B1 -> B2. I want to allow the user to:
An example of this is A being sign up steps, B being onboarding steps. Once the user finished sign up and enters onboarding, he can no longer go back to sign up. How can I achieve this? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
use
|
Beta Was this translation helpful? Give feedback.
-
I use the underlying react navigation stuff to handle that. I have not seen a way to do this with expo-router. https://reactnavigation.org/docs/navigation-actions/#reset import { useRootNavigation } from "expo-router";
import { CommonActions } from "@react-navigation/native";
// ...
function MyComponent() {
// ...
const navigation = useRootNavigation();
async function goToRoute() {
// Typescript indicates navigation can be undefined here,
// so we're implementing a graceful fallback in such cases.
try {
if (!navigation) throw new Error();
navigation.dispatch(CommonActions.reset({
index: 0,
routes: [{
// This is going to heavily rely on what your folder
// structure looks like. You can get a peek of what
// this should look like by getting the navigation.getState()
// and logging it when you're on your destination screeen
}],
}));
} catch (err) {
// graceful fallback
router.push('whatever/the/destination/is');
}
}
// ... |
Beta Was this translation helpful? Give feedback.
-
It seems the dismiss method is working
|
Beta Was this translation helpful? Give feedback.
I use the underlying react navigation stuff to handle that. I have not seen a way to do this with expo-router.
https://reactnavigation.org/docs/navigation-actions/#reset