import { useBeforeunload } from 'react-beforeunload code example
Example 1: react beforeunload
window.addEventListener('beforeunload', (event) => {
event.returnValue = `Are you sure you want to leave?`;
});
Example 2: react beforeunload
let formChanged = false;
myForm.addEventListener('change', () => formChanged = true);
window.addEventListener('beforeunload', (event) => {
if (formChanged) {
event.returnValue = 'You have unfinished changes!';
}
});
Example 3: import { useBeforeunload } from 'react-beforeunload
useBeforeunload(() => "You'll lose your data!");
Example 4: import { useBeforeunload } from 'react-beforeunload
import { useBeforeunload } from 'react-beforeunload';
Example 5: import { useBeforeunload } from 'react-beforeunload
useBeforeunload((event) => event.preventDefault());