# extendify/3.0.6/src/Launch/hooks/useWarnOnLeave.js

Extendify, version 3.0.6. 18 lines.

- Page: https://pluginprobe.com/plugins/extendify/3.0.6/code/src/Launch/hooks/useWarnOnLeave.js
- Raw: https://pluginprobe.com/plugins/extendify/3.0.6/raw/src/Launch/hooks/useWarnOnLeave.js
- Modified: 2026-02-18T22:27:14+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.6/code/src/Launch/hooks/useWarnOnLeave.js#L10-L20`.

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

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

```
