New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to go back to another stack navigator? goBack / pop doesn't go chronologically back! #6434
Comments
If anyone knows any example of achieving this, can you please leave an example here? |
Hi, don't take my word for it, but: To summarise the issue, what you want here is to have a screen accessible from any possible stack. Indeed, navigation What I did to solve this was to duplicate the screens in every stack that requires it. It's not a very clean solution, but it works(and I couldn't find an better..) Happy to hear other thoughts about this! |
Hii, I was also facing the same problem. |
@uday5162 what do you mean by interrelated? |
I've also facing same and completely agree with @walidvb only know this solution as far now.
If there is any better solution, it'll be appreciated. |
I've been struggling with this for months, to the point that I pushed screens to a reducer array (using onNavigationStateChange), and replaced the back button with a custom component to pop from that array. Still running into problems as pressing the bottom tab resets the stack and wipes crucial navigation params. I agree there needs to be a simpler pattern, I've been looking far and wide with no luck |
It seems that " This lead me to a workaround in which I simply traverse the stack navigator up until I find one that can actually go back. const navigation = useNavigation();
const goBackSafe = () => {
// Traverse parent stack until we can go back
let parent = navigation;
while (parent.dangerouslyGetState()?.index === 0 && parent.dangerouslyGetParent()) {
parent = parent.dangerouslyGetParent();
}
parent?.goBack();
}; It's a bit hacky but seems to work. In my opinion Update: Sorry, realized after posting that this bug report is for 4.x while I'm on 5 already. Maybe I need to file a separate one? |
Not ideal, but simple enough, my workaround is to pass the "back" screen to the separate stack screen: ScreenNavigatingFrom navigation.navigate('OutsideScreen', { fromScreen: 'ScreenNavigatingFrom' }) } OutsideScreen const { fromScreen, navigation } = this.props;
<BackButton
onPress={ () => (fromScreen ? navigation.navigate(fromScreen) : navigation.goBack()) }
/>
const mapStateToProps = (state, props) => ({
...props.navigation.state.params,
}); without redux, should be const { fromScreen } = this.props.navigation.state.params; and omit |
This behaviour is not going to change in v4 since it's a breaking change, but this is no longer the default behaviour in v5. So I'm closing the issue. |
As per the documentation, it has the same behavior. It didn't change at all. Any solution for v5? |
+1 |
Make a separate navigator of single screen 'AccountDetails'. Then It won't go to the 'Account' screen. Hope it'll help you. |
Using v5, wondering still no solution? |
Facing the same issue, using v5. In general, we need the same screen to be accessible from multiple locations but with the back action taking back to the screen we can from. As an example can serve any forum/community application, where the same "user profile" page could be accessed:
In all these scenarios the expected behavior is to go back to the page from where the user's profile was opened. For now, I am thinking of making any such "commonly" accessible screen a stack of its own, and include that stack in all top-level stacks of the application. I am not sure if it will work out well or, as I potentially need to include the stacks recursively one in another (profile stack is accessible from the thread stack, but at the same time a thread may be linked directly from a profile and we want to view this stack and be back to the profile we were viewing, so the thread stack should be one of the children of the profile stack...) I would appreciate any suggestions on how to tackle such cases. |
Why do you need so many stacks in the first place? If you want chronological navigation, have a single stack navigator instead of nesting multiple stacks which give you nested going back behaviour. https://reactnavigation.org/docs/nesting-navigators#best-practices-when-nesting |
Thank you for your reply @satya164 My app has quite a usual navigation pattern for a community application. It has a drawer from which different site "sections" are accessible, e.g. News, Forums, Members etc. Each "section" is represented with a Stack, which provides a typical way of accessing the screens, e.g.
There are, however, interconnections between these stacks, e.g.
Even deeper navigation cases are completely possible like And in all these cases users expect to go back to the screen from which they have opened the current screen. What do you think, could this be achieved using a single stack? If I put all these screens in a single stack, I would need to link to each screen from the drawer as a "top-level" screen, without a "back" button. I am going to give it a try soon and will update here if I succeed or what exactly prevents me from using this approach. |
Using a single stack as an only child of the drawer navigator worked for me
Now any screen is pushed on top of the stack no matter from which part of the app it is accessed. The only tricky part was to make the drawer "link" to different stacks. As the drawer navigation has only a single child node, I had to provide my custom DrawerContent, where I put links to every screen I want to be directly accessible from the drawer. Using So, I ended up using I hope this helps someone facing the same problem. |
Is there any clean solution to achieve that? |
Here is how to solve it easily: On the component, pass a param like 'returnScreen' = 'Home' (or whatever your screen name is) like this:
Then on your Screen, you can override the button to do whatever you want, here I modify the title and the back button:
|
don't forget Android back button behavior and Ios slip left exit screen .... |
Still no solution for this? Why i can't pop back to the previous screen stack? Manually it's always possible, why there is no function for that. |
Okay fam, I think I have a diagnosed this issue. To many independent Navigation Containers. The following explanation is demonstrated with hooks and typescript, but should work for other RN breeds. Hooks and TS are not a factor here. TL;DR YOU DONT NEED TO OVERIDE ANYTHING. IN FACT, YOU MAY NEED LESS CODE. REMOVE STACK OR TAB NAVIGATION CONTAINERS THAT ARE INDEPENDENT Suppose you have a root navigation. That root is going to wrapped in a NavigationContainer.
Later in your code, you are going to stack other navigators. A good example would be a left side drawer. But there is a catch, left side drawer needs to be independent, so you grant it a container with independence. Nothing too crazy about that.
Of course, being a wise and well written engineer, you create more navigators to stack into you left drawer, and grant them unique headers and bottom tab navigators. Everything is sorted out in a logical fashion. But you are not so familiar with the syntax, so you drop in another NavigationContainer, and make it independent because why not?
What you dont know is that, by making too many of your tab navigators independent, you have isolated yourself from the parent navigators. So, when you navigate from one isolated tab or stack navigation to the next, you get an error where react has no clue where to go.
This error persists even when you override the back button. You can test it with alert, and it will work, but when you try to navigation.navigate(), it fails:
vs
You may feel tempted to throw your computer out the window, but resist this temptation my friend! Instead, remove your navigation container that wraps your child navigators. By removing the containers that granted these different navigators independence, you can use the hardware back button as intended.
So please, check your code for errant Navigation Containers that are isolating your navigations. |
In react-navigation v5 you can use it's name "cangoback" and you should replace or reset screen
Good Luck! |
hi guys. would ask if you guys have resolved this problem already? im on v5 and still experiencing this issue. navigating to another tabnavigator stack screen and call goback method still goes to its parent or inital screen of navigator and not on source of navigation. which is another tab stack screen. thank you. 🙏 |
@calz10 you can do this:
when you want to open navigation.navigate('A', {initBy:'B'}) now when you are in screen const goBack = () => {
if (initBy) {
navigation.goBack()
navigation.navigate(initBy)
} else {
return navigation.goBack()
}
} |
Pass the navigator of the parent screen to the child screens. It is just like passing other objects, or functions to components. So people may scoff at this solution, but its works:
From the first navigation body
Then later in the code, if your component
Just pass the navigation from the screens and components from above the tree, down into the components that you need to use them. Just like passing data along from component to component. Easy as. |
Any solution here? |
Hey! This issue is closed and isn't watched by the core team. You are welcome to discuss the issue with others in this thread, but if you think this issue is still valid and needs to be tracked, please open a new issue with a repro. |
If I am on an ested Stack Navigator and I want to go to the parent navigator and close the state of the nested one. I did: |
Have the exact same problem - any way to achieve this in v6? |
My solution was to create a separate object of the navigation that are used and spread to every stack we need. Something like below.
|
Is this issue still persist?
|
I do not see how we could use a single stack when have a bottom tabs navigator... |
I have this issue in the same stack when I navigate back I go to home page instead of 1 screen back. |
Same issue. I'm on a screen in a stack inside a navigator. I navigate to another screen in another stack. The back button goes back to the root of the stack of the screen I navigate to and not to the screen I navigated away from. |
same here, on Android:
|
Yeah I don't get why this issue is closed when the documentation for tab navigator goes over this exact scenario. Its pretty normal to want your back behavior to go chronologically across tabs. |
There are many other issues try navigating on error boundary while using a
stack navigator you will get no navigation object.
Also things like signature for the web is unsupported. I needed to
implement normal canvas from html and it is bad way to do it.
Hope they will fix all those issues if I knew how I would.
It a shame that such good popular platform ia not supporting common issues.
בתאריך יום ו׳, 17 במרץ 2023, 00:42, מאת jeff-jacobsen <
***@***.***>:
… I do not see how we could use a single stack when have a bottom tabs
navigator...
Yeah I don't get why this issue is closed when the documentation for tab
navigator goes over this exact scenario. Its pretty normal to want your
back behavior to go across tabs.
—
Reply to this email directly, view it on GitHub
<#6434 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AGVJQ2CGPXZAF75LO2NSW4DW4OJLRANCNFSM4JIYIQUQ>
.
You are receiving this because you commented.Message ID:
***@***.***>
|
This is such a basic case - click back to go back to the previous screen that you just came from. How on earth is this not basic behavior? How is react-navigation so popular and can't handle this most basic of cases? I'm absolutely baffled. I don't even understand what the intended behavior is meant to be. This is navigation - why would you want to navigate back to anything other than the last page you just saw. Everything else should be custom/corner cases. There is a reason that this ticket is 3+ years of active discussion. Everyone is asking technical questions and trying to hack this to work like any sane navigation would. The discussion needs to switch to "what on earth is the purpose of the current behavior?" This is a super powerful tool that is lacking the most basic feature that any navigation package needs to perform - click go back to go back to the previous page you came from. |
Totally agreed with the previous orator. All of the users expecting that goBack will lead to PREVIOUS screen, no matter from what stack it was be! |
I was also wondering same thing here. It's been a 3+ years and they didn't make a progress on this issue |
Some of the solutions are nice by saying create a custom back button and handle it using some custom logic. But even if you do this and maintain the users movements in a redux array, there isn't really a way to capture or intercept navigating back using a hardware button or edge swiping, resulting in inconsistent behavior. Maybe android, but on iOS the BackHandler doesn't trigger until after navigating back completes. |
It should work fine and probably works for most.
In some cases it does not work properly.
what we did is to pass the navigation object to the header context and then
update the context with an object right before.
so every time we switch screen we update the navigation object and then the
behaviour is correct.
בתאריך יום ד׳, 14 ביוני 2023 ב-19:35 מאת jaaywags <
***@***.***>:
… Some of the solutions are nice by saying create a custom back button and
handle it using some custom logic. But even if you do this and maintain the
users movements in a redux array, there isn't really a way to capture or
intercept navigating back using a hardware button or edge swiping,
resulting in inconsistent behavior. Maybe android, but on iOS the
BackHandler doesn't trigger until after navigating back completes.
—
Reply to this email directly, view it on GitHub
<#6434 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AGVJQ2EBLSGRC3TDYHAXN7LXLHR6ZANCNFSM4JIYIQUQ>
.
You are receiving this because you commented.Message ID:
***@***.***>
|
My setup was the following:
I wasn't able to navigate back from Router Stack to Intro Stack. The error I got on all navigation -> actions was:
How I see the issue (bug) is reagrding of losing context between nested navigation containers. So the child container doesn't know about the parent. After 1 day of try and error I saw the only possible way would be to use a ref on the parent navigation container and export (forward) the current object as a function. This could be then where ever reused and fire on a navigation action, like e.q. goBack(). Example:
|
Why we could not solve this issue in all these years? It's so tiring to workaround with custom logic |
Still waiting for a proper solution... :( |
This solution seems to work for me:
For Instance :
To navigate to screen inside Wallet and still keep the previous stack in history, I do this :
|
Is there already a solution for this case? |
for some people |
The issue is closed? We're still facing the same problem |
Is there still no solution? |
For my situation & solution: Navigation Flow: Desired Navigation: Problem: Solution:
With this it solves my situation. Hope it helps someone! |
Current Behavior
I have 2 Stack navigators inside Tab navigator.
PROBLEM:
from
Dashboard Landing
screen, if I donavigation.navigate('AccountDetails');
it takes me toAccountDetails
,but while going BACK ,
navigation.goBack()
ornavigation.pop()
takes me toAccount Landing
screen, instead ofDashboard Landing
screen!Expected Behavior
navigation.goBack()
should take me to the screen I navigated from!No Solution available?
I searched StackOverflow and closed issues and I am surprised that there is absolutely no clear answer anywhere!!
Few similar questions and issues:
How to reproduce
Your Environment
The text was updated successfully, but these errors were encountered: