react native: How do I catch the rest of the buttons that the user presses and bounce the modal?

Viewed 66

How can I catch the other events that not in the on the screen in addition to onBackPress? My goal is to bounce modal when a user presses the Android back button or other buttons that not in the screen (like header and tabs) How do I catch the rest of the buttons events that the user presses and bounce modal?

useFocusEffect(
    React.useCallback(() => {
      const onBackPress = () => {
          if (isnewArray != false ){ 
        setAlertModalVisiblity(true);
        return true
          }else{ 
            return false
          }
      };
      BackHandler.addEventListener(
        'hardwareBackPress', onBackPress
      );
      return () =>
        BackHandler.removeEventListener(
          'hardwareBackPress', onBackPress
        );
    }, [])
  );
1 Answers

Seems you using react-navigation library.
I'am not sure if I understands your problem. But, if you want prevent user go back of a screen when have pendents things, like, save form informations, you should use the navigation event "beforeRemove". It is called always a android back button is pressed, back header button or navigation.goBack() is used.

Checkout the documentation, they have a example how to use it:
https://reactnavigation.org/docs/5.x/preventing-going-back

Related