react-nativereact-navigationreact-navigation-stackreact-native-gesture-handler

react-navigation enable swipe to back on android


I have a stack navigation and I want to enable swipe to go back on both android and IOS there is my code

import {
  createStackNavigator,
  StackViewTransitionConfigs,
} from "react-navigation-stack";
import HomeScreen from "../../screens/Home/Home";
import CategoryScreen from "../../screens/Category/Category";
import SubCategoryScreen from "../../screens/SubCategory/SubCategory";
import ProductScreen from "../../screens/Product/Product";

const ShopStack = createStackNavigator(
  {
    Shop: {
      screen: HomeScreen,
      navigationOptions: {
        gesturesEnabled: true,
      },
    },
    Category: {
      screen: CategoryScreen,
      navigationOptions: {
        gesturesEnabled: true,
      },
    },
    SubCategory: {
      screen: SubCategoryScreen,
    },
    Product: {
      screen: ProductScreen,
      navigationOptions: {
        gesturesEnabled: true,
      },
    },
  },
  {
    headerMode: "none",
    transitionConfig: () => StackViewTransitionConfigs.SlideFromRightIOS,

    defaultNavigationOptions: {
      gesturesEnabled: true,
    },
  },
);
export default ShopStack;


the expected behavior is when swipe go back on android like ios react-navigation version 4


Solution

  • As for v5, v6 there is an easier solution, just put those values to screenOptions in your Stack.Navigator

    gestureDirection: 'horizontal',
    gestureEnabled: true,
    ...
    <Stack.Navigator
      screenOptions={{
      gestureDirection: 'horizontal',
      gestureEnabled: true,
    }}>
    

    Those options will enable gestures on android, as well as direction will be 'horizontal' now, which is 'vertical' by default on android.