I have a tab-based app. If I'm currently on the first tab, scrolled all the way down, and I click the tab again, I want it to scroll to the top. Is this possible?
I know I should have a scroll view with a reference and then use this._scrollView.scrollTo(0)
, but how can I detect when the user taps the tabbar and decide whether it is the same tab?
tabBarOnPress in navigationOptions. Callback to handle tap events; the argument is an object containing:
the
previousScene
: { route, index }: the scene which we are leaving. thescene
: { route, index } that was tapped. thejumpToIndex
method that can perform the navigation for you.so in your case just use scene object to check which tab is pressed.
check more on react navigation documentation.
You can create your tab navigator as follow:
In order to scroll to top, you can check this solution: