Last active
March 12, 2023 17:51
-
-
Save dblazeski/1e86142e0a8548bcde9b78c71846b3f9 to your computer and use it in GitHub Desktop.
Call screen function tapOnTabNavigator when tab bar is pressed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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