question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Fix TabsRoute behaviour

See original GitHub issue

TabsRoute should implement two behaviours:

  • it should save the state for each child route and restore it when switching tabs
  • it should reset a child’s state if that child is being navigated to twice

Given the following route config:

<Router>
  <TabsRoute path="/" ...>
    <StackRoute path="/foo" ...>
      <Route path="a" ... />
      <Route path="b" ... />
    </StackRoute>
    <Route path="/bar" .../>
  </TabsRoute>
</Router>

Visiting routes should lead to the following results respectively:

  1. /foo --> show /foo
  2. /foo/a --> show /foo/a
  3. /foo/b --> show /foo/b
  4. /bar --> show /bar
  5. /foo --> show /foo/b (a tab route saves and restores its children’s state)
  6. /foo --> should reset stack of the child stack route and show /foo

Issue Analytics

  • State:open
  • Created 7 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
jmurzycommented, Jun 13, 2016

@jonathanewerner @joonhocho That is supported. Previously, there was a bug in NavigationExperimental that dropped component state when swtiching tabs. But that’s now fixed, and <Route> and its siblings should now retain component state on all navigation state updates. This includes <ScrollView> state e.g. scroll position, animation state.

UINavigationContoller extends UIViewController and uses the good old lifecycles inherited from it. It does retain all UIViewController instances that are then pushed to it. Very similar to how we do it. Both <TabsRoute> and <StackRoute> retain references to your previously focused views. Here’s how they would look when you inspect the view hierarchy.

<StackRoute> with 3 active views
      +------------+
    +-+            |
  +-+ |            |
  | | |            |
  | | |  Focused   |
  | | |   View     |
  | | |            |
  +-+ |            |
    +-+            |
      +------------+

Here a <Pop> would drop the focused view revealing the previous view in the stack with a transition animation. The views in a stack are never unmounted during a transition so you keep your component state. Transition animations can also be customized using transitionRegistry.

<TabsRoute> with 3 active views
 * +-------------+-------------+-------------+
 * |             |             |             |
 * |             |             |             |
 * |             |             |             |
 * |    Next     |   Focused   |  Previous   |
 * |    View     |    View     |    View     |
 * |             |             |             |
 * |             |             |             |
 * |             |             |             |
 * +-------------+-------------+-------------+

By default, transition animations are turned off for <TabsRoute> mirroring native behavior. But there’s nothing preventing you from making them animate when switching tabs. Aviato example does this for its inner “swticher” tabs.

<TabsRoute> is also smart enough to lazily render its tabs. So when your app launches, you will have only one of the tabs (focused view) rendered, but once you tap other tabs, all are retained until you kill the app.

Are you perhaps running into a bug we can try to reproduce and fix?

🍺

1reaction
joonhochocommented, Jun 14, 2016

Thanks @jmurzy for such detailed answer. I once noticed that tabs on react native apps did not retain previous view states, but did not know that it was fixed. It’s pretty cool to know how they can lazily render other tabs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Infinite routes behavior · Issue #281 - GitHub
Hi, I'm working on a Planning component, on a feature that allows sliding between days. I think this component can achieve this behaviour....
Read more >
URL not changing consistently in React router - Stack Overflow
The behavior I get is odd. Sometimes the URL changes as I switch between tabs. It works great. Then sometimes the URL doesn't...
Read more >
Strange router behaviour [Ionic 6 Vue]
Hello. I am experiencing some weird behavior when trying to create a route with an optional parameter. Here is the router code:
Read more >
Router tutorial: tour of heroes - Angular
Behind this behavior is the router's canDeactivate guard. ... how to create multiple routing modules and import those routing modules in the correct...
Read more >
Navs - Bootstrap
JavaScript behavior. Use the tab JavaScript plugin—include it individually or through the compiled bootstrap.js file—to extend our navigational tabs and pills ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found