Skip to content

Instantly share code, notes, and snippets.

@dblazeski
Last active March 12, 2023 17:51
Show Gist options
  • Select an option

  • Save dblazeski/1e86142e0a8548bcde9b78c71846b3f9 to your computer and use it in GitHub Desktop.

Select an option

Save dblazeski/1e86142e0a8548bcde9b78c71846b3f9 to your computer and use it in GitHub Desktop.
Call screen function tapOnTabNavigator when tab bar is pressed
// When the tab bar is pressed and the screen is focused, check if the screen component has tabOnTabNavigator function and call it
// For the screen function see https://gist.github.com/dblazeski/e7677db0d209c03713b24fcf00443c0e
const getScreenRegisteredFunctions = navState => {
// When we use stack navigators.
// Also needed for react-navigation@2
const { routes, index, params } = navState;
if (navState.hasOwnProperty('index')) {
return getScreenRegisteredFunctions(routes[index]);
}
// When we have the final screen params
else {
return params;
}
}
const TabsNavigator = createMaterialTopTabNavigator(
{
Home: Home,
Profile: Profile,
Search: Search,
},
{
defaultNavigationOptions: ({ navigation }) => ({
tabBarOnPress: ({ defaultHandler }) => {
if (navigation && navigation.isFocused()) {
const screenFunctions = getScreenRegisteredFunctions(navigation.state);
if (screenFunctions && typeof screenFunctions.tapOnTabNavigator === 'function') {
screenFunctions.tapOnTabNavigator()
}
}
// Always call defaultHandler()
defaultHandler();
},
})
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment