Is there a way to conditionally disable the swipe to go back gesture in react native iOS?

Viewed 6792

I'm looking for a way to conditionally disable the swipe to go back gesture in react native iOS. I'm using the react-navigation library to control navigation. For Android, I am able to do it using BackHandler. Is it possible to do something similar in iOS?

  componentDidMount() {
      BackHandler.addEventListener("hardwareBackPress", this.handleBackButton);
  }

  handleBackButton = () => {
    if (this.props.creating) {
      return true; // Disables the back button in Android
    }
  };

  componentWillUnmount() {
      BackHandler.removeEventListener(
        "hardwareBackPress",
        this.handleBackButton
      );
  }
3 Answers

But I'm trying to only disable gestures when this.props.creating is true.

Try adding this static method to your component.

   static navigationOptions = props => {
      return {
        gesturesEnabled: this.props.creating ? false : true
      }
    };

StackNavigator takes navigationOptions representing the "Default navigation options to use for screens"

Example:

 const RootStackNavigator = StackNavigator(
  {
    Login: {
      screen: LoginScreen
    },
    Main: {
      screen: MainScreen
    }
  },
  {
    initialRouteName: 'Login',
    navigationOptions: {
      gesturesEnabled: false   // <- add this line
    }
  }
);
Related