1.1 KiB
1.1 KiB
title, type, tags, cover, dateModified
| title | type | tags | cover | dateModified | |||
|---|---|---|---|---|---|---|---|
| React useUnload hook | snippet |
|
digital-nomad-14 | 2020-11-29T14:16:36+02:00 |
Handles the beforeunload window event.
- Use the
useRef()hook to create a ref for the callback function,fn. - Use the
useEffect()hook andEventTarget.addEventListener()to handle the'beforeunload'(when the user is about to close the window). - Use
EventTarget.removeEventListener()to perform cleanup after the component is unmounted.
const useUnload = fn => {
const cb = React.useRef(fn);
React.useEffect(() => {
const onUnload = cb.current;
window.addEventListener('beforeunload', onUnload);
return () => {
window.removeEventListener('beforeunload', onUnload);
};
}, [cb]);
};
const App = () => {
useUnload(e => {
e.preventDefault();
const exit = confirm('Are you sure you want to leave?');
if (exit) window.close();
});
return <div>Try closing the window.</div>;
};
ReactDOM.createRoot(document.getElementById('root')).render(
<App />
);