react-router-navigation icon indicating copy to clipboard operation
react-router-navigation copied to clipboard

No docs on onRequestChangeTab and onReset

Open mihir0x69 opened this issue 7 years ago • 10 comments

Is there any documentation other than this on onRequestChangeTab and onReset.

What I want to do is just call this.props.history.push('/my/path') from a tab. But it throws an error saying- Invariant violation: there should always be only one screen active, not 0.

How do I navigate to a different route from a Tab?

mihir0x69 avatar Dec 26 '17 12:12 mihir0x69

You can see the usage in the App.js of the example/huge on line #62

From my understanding its used when a <Tab /> gets activated and want to set a sub route for the tabs child component. Here /profile/likes and /profile/bookmarks, so when the <Tab component={Profile} /> is active onRequestChangeTab will be called and you can set the sub route so a active tab can be determined.

Can you past a bit more of the code so maybe you need something else?

LaszloDev avatar Dec 29 '17 17:12 LaszloDev

@LaszloDev Hi, thanks for pointing me to the example app. I think the example works because the base path remains the same- /profile/. What if I want to navigate away to a completely different route, say /settings/account?

mihir0x69 avatar Jan 02 '18 06:01 mihir0x69

In general changing the route with react-router work by calling history.push("/new/route") or rendering <Redirect to="/new/route"/>. When you want, for example, to have a button with a callback you can call history.push("/new/route") to change the route. I do have some issues with changing the route together with <Tabs />, but didn't had time to look more into it. Can you explain a bit more what you are trying to archive?

LaszloDev avatar Jan 02 '18 18:01 LaszloDev

@LaszloDev My bad! I was trying to navigate to a route /profile, but inside the Profile component there was a Navigation component, with 2 routes. I only had to specify a fully qualified path. I never bothered to open Profile. Thanks for entertaining me. Closing.

mihir0x69 avatar Jan 03 '18 06:01 mihir0x69

But the docs are still missing. If you want I can write something up and send a pr.

mihir0x69 avatar Jan 03 '18 06:01 mihir0x69

Would be great!

LaszloDev avatar Jan 03 '18 10:01 LaszloDev

@LaszloDev Thanks for your replies ! @KarandikarMihir Would be great !


To explain the behaviour of onRequestChangeTab let's take the following example:

<Tabs>
  <Tab path="/" component={Index} />
  <Tab path="/(profile|bookmarks)" component={ProfileBookmarks} />
</Tabs>

From the tabbar, if you click on the 2nd tab, react-router-navigation is not able to determine the route of the history to push because /(profile|bookmarks) is a regex. You are then required to set the action history.replace(pathname: string) with the prop onRequestChangeTab() to set whether you want to go to /profile or /bookmarks.

So you have to do the following:

  <Tabs>
    <Tab path="/" component={Index} />
    <Tab
      path="/(profile|bookmarks)"
      component={ProfileBookmarks}
      onRequestChangeTab={() => history.replace('/profile')}
    />
  </Tabs>

onReset prop is a callback which resets the current tab. If you're using a <FlatList /> inside a <Tab /> component, onReset prop can be used to scroll to the top of the list if you tap twice to tab. Take a look at this example ! Another use case with a tab system inside a <Tab />.

LeoLeBras avatar Jan 03 '18 13:01 LeoLeBras

@LeoLeBras Cool. I'll try to finish this by tomorrow!

mihir0x69 avatar Jan 07 '18 12:01 mihir0x69

Great !

LeoLeBras avatar Jan 07 '18 12:01 LeoLeBras

@KarandikarMihir Any update on this work?

LeoLeBras avatar Mar 10 '18 12:03 LeoLeBras