Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

react-native material top tab navigator swipe disable depending on screens

Wanna make only Map component swipe-disabled but the entire screens were applied when using "swipeEnabled".

How can I do?

const Tab = createMaterialTopTabNavigator();

const Tabs = () => {

  return (
    <Tab.Navigator
      swipeEnabled={false}  // <- Screens can be swiped but it is applied to every screen.
      {...}
    >
      <Tab.Screen
        name="Home"
        component={Home}
      />
      <Tab.Screen
        name="Map"
        component={Map}
      /> 
    </Tab.Navigator>
  );
}

const App = () => {
  return (
    <NavigationContainer>
      <SafeAreaView style={styles.safeAreaView} />
      <Tabs />
    </NavigationContainer>
  );
}
like image 519
Dyobi Avatar asked Oct 16 '25 15:10

Dyobi


1 Answers

You could pass a state value to swipeEnabled and update the value to false if you're on the Map screen like this:

const Tab = createMaterialTopTabNavigator();

const Tabs = () => {
  const [swipeEnabled, setSwipeEnabled] = useState(true);
  return (
    <NavigationContainer>
      <Tab.Navigator
        swipeEnabled={swipeEnabled}
        screenOptions={({ navigation, route }) => {
          if (route.name === 'Map' && navigation.isFocused()) {
            setSwipeEnabled(false);
          } else if (route.name !== 'Map' && navigation.isFocused()) {
            setSwipeEnabled(true);
          }
        }}>
        <Tab.Screen name="Home" component={Home} />
        <Tab.Screen name="Map" component={Map} />
      </Tab.Navigator>
    </NavigationContainer>
  );
};
like image 110
Bas van der Linden Avatar answered Oct 19 '25 12:10

Bas van der Linden



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!