How to disable react native navigation conditionally?

Viewed 68

I would like to disable navigation based on certain condition in my screen so that user stays in the screen without being able to navigate to other screens untill the specific task in the screen has completed. can anyone guide me how do i achieve disabling and enabling navigation conditionally?

I have looked at the documentation here https://reactnavigation.org/docs/preventing-going-back/ but i cannot disable navigation when

  1. i'm using the state variable as condition .

  2. This way only disables back button navigation. i would like to disable the entire navigation(tab navigation etc)

const [hasRecordingStarted, sethasRecordingStarted] = useState(false);
const navigation = useNavigation();

  useEffect(() => {
    
    navigation.addListener('beforeRemove', e => {
      if (!hasRecordingStarted) {
        return;
      }
      e.preventDefault();
      Alert.alert(
        'You cannot leave the screen while recording',
        'To leave the screen stop and save or discard the route recording',
      );
    });
    
  }, []);
1 Answers
  1. This way disables back button navigation or you can also use BackHandler from react-native to do that
  2. You can put conditions on your navigation.navigate() or navigation.replace() or whatever programmatic navigations.
  3. Disable the swipe gesture for the screen (gestureEnabled: false) in navigationso that in iOS it prevents swipe back navigation.
Related