Replies: 27 comments 48 replies
-
I want this too. In the official example, The |
Beta Was this translation helpful? Give feedback.
-
What I'm doing as workaround to bypass this: function clearHistory() {
const state = navigation.getState();
navigation.reset({
...state,
routes: state.routes.map(route => ({ ...route, state: undefined }))
});
} It'll have the similar effect as |
Beta Was this translation helpful? Give feedback.
-
Should be doable with the Would urge caution against mutating history too much though. Very easy for this to clash with other behaviors and it violates user expectations. There are valid reasons to use reset but often as not this reflects an underlying dynamic that could be addressed through less convoluted means, like redirects or shared routes or conditional rendering. |
Beta Was this translation helpful? Give feedback.
-
it's doable but not safe as you said, would be better to have something handled by expo-router to have proper state. |
Beta Was this translation helpful? Give feedback.
-
This is table stakes for even a simple app. |
Beta Was this translation helpful? Give feedback.
-
so is there a way to prevent user slide back to login page ? |
Beta Was this translation helpful? Give feedback.
-
Just to clarify (or propose) a valid and common use cases: |
Beta Was this translation helpful? Give feedback.
-
any chance we get a comment from the expo team is they plan to add this or if we need to do custom stuff forever? because expo 49 upgrade broke the onboarding of my app where i was doing a custom reset using the rootNav.reset :/ |
Beta Was this translation helpful? Give feedback.
-
is there any current way, hacky or not, that i can reset the navigators? I have tabs, each with their own stack of pages, and I need to reset everything when a user switches "teams", changing the content and access that each tab and stack page shows |
Beta Was this translation helpful? Give feedback.
-
I've had some success using: while (router.canGoBack()) { // Pop from stack until one element is left
router.back();
}
router.replace(newPath); // Replace the last remaining stack element It isn't perfect, but seems to work most reliably out of options provided in this thread, at least for my use case. |
Beta Was this translation helpful? Give feedback.
-
the way i clear the nav history is like this.
|
Beta Was this translation helpful? Give feedback.
-
can we get anyone from the expo team to actually answer? or give something, anything? we have legit use cases for this for many people and still no one seem to care :/ It's a bit frustrating at times to never get help/answers, here or on discord :/ Sorry a bit frustrated that ticket is almost a year old soon and no one ever bothered to give insight if that's something that will be done or will never happen. If you decided to not listen to community that's fine but then let us know so we know where we stand and that it's a close project(close ideas in discussions then) and we know we need to make support expo ticket for paid users to get answers :) |
Beta Was this translation helpful? Give feedback.
-
i was facing a similar issue, i solved it by re-structuring my projects app folder like this The issue i was facing was that i couldn't reset the navigation after i completed my onboarding screens, but if i disabled gestures in the main layout, the customer won't be able to navigate back, which pretty much does the same job as a navigation reset would do in this situation. here's my old project structure
So, i created a new route grouping called
this seems to work as i expect, after my This is ofc not a universal substiture for |
Beta Was this translation helpful? Give feedback.
-
@wcastand import { useNavigation } from 'expo-router'
import { CommonActions } from '@react-navigation/native'
export default function Screen() {
const navigation = useNavigation();
const handleResetAction = () => {
navigation.dispatch(CommonActions.reset({
routes: [{key: "(tabs)", name: "(tabs)"}]
}))
}
return (
<>
{/* ...rest of the code */}
<Button title='Reset' onPress={handleResetAction} />
</>
);
} |
Beta Was this translation helpful? Give feedback.
-
why is this hard, the above example does something but not what most use cases would desire. Thanks for the post but Expo that hardly helps |
Beta Was this translation helpful? Give feedback.
-
This worked for me: #495 (reply in thread) |
Beta Was this translation helpful? Give feedback.
-
moved the discussion to the expo repo |
Beta Was this translation helpful? Give feedback.
-
In my case, I needed to resolve that same cenario that some people was struggling as well.
I will share my solution in case it helps someone with de same issue. I changed the way that I was structuring my routes and _layouts, before it was something like this
The problem with that approach was that Stack on the root layout, because when I navigate from the auth pages to the home, I needed to reset the Stack state, the same issue for a logout flow redirecting from home to signIn. Now, I'm using this approach:
Using a Slot and creating different Stacks for different flows solved my problem. When I call router.navigate from signIn to the home I'm changing the Stack instance that is rendered, then I don't have to reset. |
Beta Was this translation helpful? Give feedback.
-
My solution is similar to @Mik1337, using
|
Beta Was this translation helpful? Give feedback.
-
Im using firebase auth so this code snip wont work for everyone, but conceptually I solved this by making sure on every screen I check the users authentication status at the start of every screen.
Im also using expo router to navigate and on the sign in screen I just navigate with a These two things seems to create a decent experience that way if a user does try and use back after a sign out it just routes them right back to my sign in screen. It's working well so far and gets around the issue of a poor navigation experience for my users. Would be awesome to see expo router have a clear history option though to start the navigation history over after a sign in or a sign out. Seems like a great opportunity to encourage more router usage vs have to get this type of feature with react native navigation. |
Beta Was this translation helpful? Give feedback.
-
I've found this solution helpful: expo/expo#28485
|
Beta Was this translation helpful? Give feedback.
-
Just note that @Re1nGer's solution will fail in case import { router as expoRouter } from 'expo-router'
import type { ExpoRouter } from 'expo-router/types/expo-router'
import type { RouteName } from './routeNames'
interface Router extends ExpoRouter.Router {
reset(route: RouteName): void
}
export const router: Router = {
...expoRouter,
reset(route: RouteName) {
if (router.canGoBack()) router.dismissAll()
router.replace(route)
},
}
// Usage example
import { router } from '@router'
router.reset(RouteName.Home) |
Beta Was this translation helpful? Give feedback.
-
Hi there,
I have requirements to implement behaviour: I'm on index page, when click on button I redirect to page-3. But when I click on back button (I use custom back button, and use router.back()) I would like to get to page-2 -> page-1 -> index. Is this possible or not? Thank you |
Beta Was this translation helpful? Give feedback.
-
any reason to use |
Beta Was this translation helpful? Give feedback.
-
I can't use router.replace('/'), this don't work. It's so importat has reset method in expo-router. I needed to use router.navigate(/) and disable hardwareBackPress in my first screen, it's so bad. |
Beta Was this translation helpful? Give feedback.
-
Worked for me:
|
Beta Was this translation helpful? Give feedback.
-
Both of the code chunks that people provided worked for me in the end, but the
|
Beta Was this translation helpful? Give feedback.
-
I need to reset my route quite often and being forced into using the navigation to reset means i don't have expo-router logic and need to do it by hand (tabs, sub stack, etc)
having a router.reset() would be amazing.
doc suggest to use
replace
but it's not the same thing, replace doesn't clear the history.Beta Was this translation helpful? Give feedback.
All reactions