Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tabBarOnPress Not Available in React Navigation v5

I'm using React Navigation v5 with @react-navigation/bottom-tabs and my tabs look something like this.

<NavigationContainer>
  <Tab.Navigator>
    <Tab.Screen name="Home" component={HomeScreen} />
    <Tab.Screen name="Modal" component={ModalScreen} />
    <Tab.Screen name="Settings" component{SettingsScreen} />
  </Tab.Navigator>

I want to open screens on Home & Settings tab but on the Modal Tab, I would like to open an modal and for that, with React Navigation v4, it is possible using tabBarOnPress which will run the callback but that is not available in React Navigation v5, is there any alternative for tabBarOnPress with React Navigation v5?

Any help would be appreciated!

like image 499
Muhammad Yaqoob Avatar asked Feb 25 '20 13:02

Muhammad Yaqoob


1 Answers

You need to use tabPress event:

<Tabs.Screen
  name="Modal"
  component={ModalScreen}
  listeners={{
    tabPress: e => {
      // Prevent default action
      e.preventDefault();
    },
  }}
/>

https://reactnavigation.org/docs/bottom-tab-navigator#events

https://reactnavigation.org/docs/navigation-events#listeners-prop-on-screen

like image 79
satya164 Avatar answered Nov 19 '22 11:11

satya164