useUnload
useUnload registers a beforeunload handler on window for as long as the component is mounted. Use it to warn
about unsaved changes before the tab closes, reloads or navigates to another document.Hooks
Signature
useUnload(handler: (e: Event) => string | undefined | void): void;
Name | Type | Description |
|---|---|---|
handler | (e: Event) => … | Runs on beforeunload. The latest version runs; the listener is attached once. |
returns | void |
Scrolls sideways inside the table. The page does not.
Example
import { useUnload } from '@apollion-dsi/core/hooks';
function UnsavedGuard({ dirty }: { dirty: boolean }) {
useUnload((e) => {
if (!dirty) return;
e.preventDefault();
(e as BeforeUnloadEvent).returnValue = '';
});
return null;
}Pitfalls
- It does not see in-app (client-side) navigation. Use your router's blocker for that.
- Browsers show their own generic confirmation text; a custom message is ignored.
beforeunloaddoes not wait for promises. Do not start async work that must finish.