-
-
Notifications
You must be signed in to change notification settings - Fork 180
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 reuse the same screen across tabs in a bottom tabs navigator? #110
Comments
I knew the day would come where someone asks this...and yes, I totally agree. I have an answer implemented in my own app. And it's all possible thanks to solito's (undocumented) So I'm sharing this code as-is. I'm happy to talk through it a bit to explain it. Here's the gist: And so, when you provide a custom In my example below, I copy-pasted Hope that all makes sense. Here you go...
|
Thank you! |
If you have bottom tabs nested, you’d just have to adjust the |
if (!parent) {
break
} becomes: if (!parent || parent.type == ‘tab’) {
break
} (this assumes you only have one tab navigator) |
As I understand the problem, this is referring to past history states per tab, like each is it's own browsing experience like how twitter does it? And the solution depends on where you need to jump to basically? |
The problem is: you’re on spotify’s home screen. You open an artist. You expect it to open on the same tab. But your linking config has it configured to open on the search tab, so it jumps over. The solution is to overwrite the navigation action to stay in the same tab. My solution makes some assumptions about your stack navigators: namely, that every screen can be opened in every tab. This is the way I organized my stacks. To achieve this, I have one stack navigator component, wrapped into its own component. Each tab renders the same component with a different |
Thank you for going a little more into detail. How would you suggest we set up the linking config in this case? Especially in terms of avoiding duplicate urls? |
The solution I shared doesn't require you to duplicate URLs, since it takes a single URL and overwrites the action to stay in the current tab. You can put the urls inside of any tab technically, assuming every tab has every screen the way our app does. Which is to say: the only time the tab in your linking config would matter is on a deep link from outside the app. Does that make sense? |
@nandorojo Makes perfect sense, thank you. |
This is how Twitter and Facebook does it. But I’d like to make the case for the other way: that content navigation stays in the Home tab. Like react-navigation v6 and Solito does by default. Since if you can start content navigation inside the notifications tab, and then switch tabs, the notifications tab has now lost its ability to take you directly to notifications (you’d have to switch to it and then navigate all the way back up the content stack to see notifications again). This happens frequently in my experience. I think the problem with switching tabs when clicking a notification has to do more with transitions, since when I’ve seen it done before (not in Solito necessarily) it can be confusing, because you see a flash of the old content in the Home tab first, before the new screen is put on top. Such transitions gives a feeling of «yank» and is disorienting (compared to no animations, for instance). But if the content is immediately switched in the Home tab (with option to click back, to see the previous content & state there), then it could work quite well. |
It’s all pretty app-dependent, which is why Solito doesn’t do it by default. In most cases I don’t want to switch tabs, unless I’m going to the first screen of a given tab’s stack. In my experience, animations between tabs with react navigation result in frame drops, even when using Reanimated. |
that’s true, unless you used some sort of |
So I've been playing around with just bottomtabs as the root navigator and the solution works great. One thing I had to do though was turn off state persistence in the navigation container. For some reason if it was turned on, the When the bottomtabs are nested inside a root native stack however, I'm still not able to get it to work even with the modified code you've graciously provided. Not sure what is going wrong there. |
Mmm I'm not exactly sure how that could affect this, since
Can you share your code? Did you try logging a bit to see when it reaches a tab navigator? What happens exactly, does it just not navigate or does it stay in the same stack? |
Out of curiosity, why would the tabs go inside of a native stack? Is the order stack → tabs → stack? |
@nandorojo correct. The root stack is basically just where I would put tab-independent modals such as "create-post", "signin-screen" or "404-screen". But in trying to answer your question I just realized there is no real need for me to have a root stack. I can just add all the screens to the same stack nested inside the bottom tabs, and have the bottom tabs at the root with no issues. In terms of what does happen when you have the Not sure if it's still worth pursuing a fix for that. Thanks again for all your help 😊 . |
That's right. Before native stack we couldn't do this, but now, modals will show on top of everything, so there's no need for a root stack. It's generally recommended to keep navigation flat when possible. |
Is this the case for Android? I cannot figure out how to get the native stack to behave this way for Android (after I flattened my stack hierarchy 😅) |
Hi @nandorojo,
My app is entirely built with Solito and am loving the developer experience so far.
I'm just encountering one roadblock at the moment which is:
Let's say you have a standard bottomtabs navigator with the following tabs:
Most of these these tabs will be reusing the same screens such as
<PostScreen />
or<UserScreen />
in the HomeStack, SearchStack, etc...So let's say you're in the Notifications tab and you navigate to a
<PostScreen />
, ideally you'd want to stay inside that notifications tab and preserve your state in all the other tabs.However in
react-navigation
v6, the app yanks you back to the Home tab, which has the same instance of that<PostScreen/>
as part of the HomeStack.The obvious solution is to make a separate
<PostScreen/>
and<UserScreen/>
for each tab.But I fear it's going to drastically increase the complexity of the code, and I will end up regretting this.
How would you proceed in such a scenario?
Thanks!
The text was updated successfully, but these errors were encountered: