# extendify/3.0.4/src/AutoLaunch/hooks/useWarnOnLeave.js

Extendify, version 3.0.4. 20 lines.

- Page: https://pluginprobe.com/plugins/extendify/3.0.4/code/src/AutoLaunch/hooks/useWarnOnLeave.js
- Raw: https://pluginprobe.com/plugins/extendify/3.0.4/raw/src/AutoLaunch/hooks/useWarnOnLeave.js
- Modified: 2026-02-26T23:48:52+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/extendify/3.0.4/code/src/AutoLaunch/hooks/useWarnOnLeave.js#L10-L20`.

```javascript
import { useEffect } from '@wordpress/element';

export const useWarnOnLeave = (enabled = true, callback) => {
	// Display warning alert if user tries to exit
	useEffect(() => {
		if (!enabled) return;
		const handleUnload = (event) => {
			event.preventDefault();
			callback?.();
			event.returnValue = '';
			return '';
		};
		const opts = { capture: true };
		window.addEventListener('beforeunload', handleUnload, opts);
		return () => {
			window.removeEventListener('beforeunload', handleUnload, opts);
		};
	}, [enabled]);
};

```
