Ionic React: Close modal / alert on back button pressed

One way would be to call the dismiss method on the modal when listening for the backButton event.

import { Plugins, Capacitor } from '@capacitor/core';
import { useEffect } from 'react';
...

const App: React.FC = () => {

  useEffect(() => {
    if (Capacitor.isNative) {
      Plugins.App.addListener('backButton', e => {
        if (currentModal !== null) {
          currentModal.dismiss();
        }
      })
    }
  }, []);

...
1 Like