Skip to content

How to replace multiple routes to prevent user from going back? #880

Answered by GibbyBox
lsps9150414 asked this question in Q&A
Discussion options

You must be logged in to vote

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 o…

Replies: 3 comments 4 replies

Comment options

You must be logged in to vote
1 reply
@lsps9150414
Comment options

Comment options

You must be logged in to vote
3 replies
@wcastand
Comment options

@GibbyBox
Comment options

@wcastand
Comment options

Answer selected by lsps9150414
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
5 participants