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
Allow TabBarBottom
accept onTabPress
callback (#486)
#525
Conversation
I don't think this is the right way to do it. How would you wire it up to with the correct rendered navigator instance when you have multiple navigators mounted? Seems impossible with this approach. |
@satya164 I'm thinking dispatching an action to a Redux store from |
@kimdhoe yes, but say you have the navigator rendered in two places, for example it might just be tabs nested inside a stack, then how do you know which one sent the event? |
@satya164 Hmm.. I thought I could use route names for that. Wouldn't it be possible if I use unique names for all screens? |
Route names are unique per navigator. If you have the same navigator rendered in two places, they will have same route names. |
I hadn't thought that far ahead. It would be tricky to make a correct view to scroll to top in such complicated situations. In Redux store or context, I'd have to keep track of route names and refs to ScrollViews in the last mounted tab navigator, which would be visible at a moment. The structure will be something like LIFO stack. Whenever a new tab navigator is mounted, new data will be pushed to the stack, and poped from it on unmounting. Then, when a route name is sent from But even if such fine control is impossible, IMHO adding |
I'm sure that fine control is possible if the event listeners could be specified as part of screen configuration rather than being defined when navigator is defined. It's a common pattern and shouldn't require redux or complicated logic to achieve. |
I have a similar requirement, where I would like to dispatch a navigation action whenever the active screen's tab button is pressed. @satya164 I believe that would handle the ambiguous case of multiple Navigators, and passing navigation instances to callbacks seems to be a common pattern. |
Ah, I see. I have come to something like this: class RecentChatsScreen extends React.Component {
static navigationOptions = {
tabBar: () => {
onTabPress: (scene: TabScene) => {
if (scene.focused) {
// scroll to top please
}
}
}
};
} But I still had to manually manage a stack of ScrollView refs to implement scroll to top. |
…ct-navigation#486) Pressing a tab bar often requires some kind of side effects other than navigation to be performed. This PR will allow a screen accept `onTabPress` callback as an option. It will be invoked exactly once on every press on the screen's corresponding tab button, receiving a `TabScene` object as an argument.
Updated the PR. This will allow: class RecentChatsScreen extends React.Component {
static navigationOptions = {
tabBar: {
onTabPress: ({ route, index, focused }: TabScene) => {
if (focused) {
alert('active tab pressed.');
}
}
}
};
// ...
} |
@satya164 can you review this again? I could really use this functionality in my app 💯 |
@satya164 i'd be keen for this too :) |
+1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there anything holding this back? It seems a very reasonable implementation.
My use case is to have a tab icon that simply opens a modal (e.g. how camera is opened in Instagram). For this use case, I'd need to be able to prevent the default Thought I'd throw that out there. |
+1 |
3 similar comments
+1 |
+1 |
+1 |
@kimdhoe Seeing as the proposed callback doesn't have access to |
Somehow I missed this when searching open PRs (searched I've implemented onPress consistently for both |
Pressing a tab bar label often requires performing some kind of side effects
other than navigating: Scroll-to-top functionality, for instance.
At the moment
TabView.TabBarTop
, which is the default on Android,handles
onTabPress
callback as expected, butTabView.TabBarBottom
ignores it.
This PR addresses the problem so that
TabView.TabBarBottom
can acceptand handle the optional
onTabPress
callback. The callback is invokedexactly once if present on every tab bar press, accepting as a sole
parameter a
NavigationRoute
, which is the route object the pressedtarget represents.
Fixes #486.