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

China☆狼群 提交于 2021-02-05 08:39:06

问题


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>
  );
}

回答1:


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>
  );
};


来源:https://stackoverflow.com/questions/63611393/react-native-material-top-tab-navigator-swipe-disable-depending-on-screens

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!