Create new file:
import {BackHandler} from 'react-native';
/**
* Attaches an event listener that handles the android-only hardware
* back button
* @param {Function} callback The function to call on click
*/
const handleAndroidBackButton = (callback: any) => {
BackHandler.addEventListener('hardwareBackPress', () => {
callback();
});
};
/**
* Removes the event listener in order not to add a new one
* every time the view component re-mounts
*/
const removeAndroidBackButtonHandler = () => {
BackHandler.removeEventListener('hardwareBackPress', () => {});
};
export {handleAndroidBackButton, removeAndroidBackButtonHandler};
and in your main render file (app.tsx) add the following code:
import {handleAndroidBackButton, removeAndroidBackButtonHandler} from "./utils/back-button.handler";
public componentDidMount(): void {
handleAndroidBackButton(false);
}
this.navigatoris undefined. Please check it. - Sagar Khatri