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
Ability to define a custom onPress
for a tab
#314
Comments
CC: @satya164 |
Just do |
Ah I see you mean when pressing tab icon. We should allow adding a listener to tab press so you can perform custom actions. cc @ericvicenti |
I was also wondering this, is there no way of listening to the actions from the TabNavigator button press without hooking onto the actions they produce? |
I agree, a tab button handler override sounds like a reasonable thing to add, maybe call it |
@ericvicenti it won't be very useful inside the static |
Ok, so you want it to have access to the tab navigator's props? What API would you recommend for this? Usually I would just fire a different action other than navigate, so I think the dispatch channel would be fine for me, (plus I may want to do side effects). |
I think @browniefed wanted access to tabview's
yeah, if you are not using redux and say just want to fetch some data, or component state, or say scroll to top, you need to be able to listen to press event from inside the component. |
FWIW, I think the best practice for |
I don't disagree but I'd like to potentially do logic to determine if I need to trigger |
I am fairly new to this new (and so far awesome) nav project so take my words with a grain of salt, but the basic behaviour described by OP is definitely a common behaviour among production apps currently in the market. Facebook, Instagram, Reddit (official app), Twitter, and AirBNB are just a few popular examples of this behaviour. The common "tap flow" between these apps, when you are currently on the tab you are tapping:
Just some thoughts to keep in mind when architecting a solution :) |
I need this too! Any ideas for a workaround? |
Dear @satya164 will this one be approached any time soon? Otherwise I may be forced to build my own TabNavigator. And I'd really prefer not to :) Thanks! |
I'm running into this problem now, it would be really helpful if the tabBarComponent took navigator as a prop so I can dispatch actions. |
I'm pretty sure it gets |
I pretty sure it doesn't unless I am not understanding something. If I print the props my tabBarComponent gets I get:
Code I used to get that list: http://pastebin.com/EV9gdjws |
Don't know what version you're on, but we pass the prop https://github.com/react-community/react-navigation/blob/master/src/views/TabView/TabView.js#L136 So maybe try |
Ah, you are right, it is passed in on master but not 1.0.0-beta.5. Thanks a lot. |
@satya164 when is this going to be implemented? |
This is the workaround I use. Feedback welcome. const MainTab = StackNavigator({
Home: {
screen: MyHomeScreen,
},
Profile: {
screen: MyProfileScreen,
},
Related: {
screen: MyRelatedScreen,
},
});
let currentIndex;
const StacksInTabs = TabNavigator(
{
MainTab: {
screen: MainTab,
},
SettingsTab: {
screen: SettingsTab,
},
},
{
tabBarComponent: ({ jumpToIndex, ...props }) => (
<TabView.TabBarBottom
{...props}
jumpToIndex={index => {
if (currentIndex === index && index === 0) {
let resetTabAction = NavigationActions.navigate({
routeName: "MainTab",
action: NavigationActions.reset({
index: 0,
actions: [NavigationActions.navigate({ routeName: "Home" })],
}),
});
props.navigation.dispatch(resetTabAction);
} else {
currentIndex = index;
jumpToIndex(index);
}
}}
/>
),
}
); |
@maxmcd Nice workaround, although gets caught out if you use Would be great to have real support for this built in, where we can just access a |
onPress
for a tab
Any update on when this will be available? |
Would it be possible to have the current understanding on this problem? I tried to read the comments but it seems to be a succession of false leads (or incomplete...). |
No? |
Is this a right way to capture the on press event.
This worked for me ! |
@jitinmaher I believe that will only put an |
Any update on this? |
I experimented a little with this and came up with this pretty clean solution. The tab bar button will function as the back button when it is the active tab. It requires that you also imports the following:
|
@tobiassernhede that's a neat trick, but there's already a real working solution: #1335 |
@cooperka This issue should be closed. |
Hi @dmr07, unfortunately I'm not a maintainer so I don't have permission to close this issue. |
If we are deep into a navigation stack and want to jump back to the home screen, there is currently no way to do that except by pressing back several times. This change listens for presses on the currently active tab and will reset the stack back to the home route.
@maxmcd you saved my life today 👍 |
Hi! In an effort to get the hundreds of issues on this repo under control I'm closing issues tagged as questions. Please don't take this personally - it's simply something we need to do to get the issues to a manageable point. If you still have a question I encourage you to re-read the docs, ask on StackOverflow, or ask on the #react-navigation Reactiflux channel. Thank you! |
TabNavigator({
****
}, {
tabBarComponent: ({jumpToIndex, ...props}) => (
<TabBarBottom
{...props}
getOnPress={() => {
return (scene) => {
const {route, index, focused} = scene;
if (index !== 3 /* ~ ~ */) {
jumpToIndex(index);
} else {
// HANDLE IT !!
}
};
}}
/>
),
tabBarPosition: 'bottom'
}) Worked for me!! |
@spencercarli can you please tag this issue as |
Hi @pensierinmusica, this was already solved in #1335. The feature request has been accepted. |
@cooperka oops I missed that, thx, awesome then! :) |
All you need to do is set a custom onPress event for the TabBarIcon, as follows:
Hope it helps! |
@cooperka That's really helps. Thanks |
One more solution...
|
@jamesisaac this solution is more exquisite! really thanks to me! |
For anyone looking for an easier solution using React Navigation 6:
|
Hey! This issue is closed and isn't watched by the core team. You are welcome to discuss the issue with others in this thread, but if you think this issue is still valid and needs to be tracked, please open a new issue with a repro. |
=== by @grabbou
This is a catch-all issue for TabBar
onPress
, that is:=== original issue below ===
Given following nested tab setup
How to back/reset to Home screen from Related screen on press MainTab icon in tabs when MainTab is focused?
The text was updated successfully, but these errors were encountered: