web-base/react/shared/hooks/before-unload.js

16 lines
440 B
JavaScript
Raw Normal View History

2023-03-05 15:30:06 +01:00
import {useCallback, useEffect} from "react";
export default function useBeforeUnload(modified) {
const capture = useCallback((event) => {
if (modified) {
event.preventDefault();
}
}, [modified]);
useEffect(() => {
window.addEventListener("beforeunload", capture, {capture: true});
return () => window.removeEventListener("beforeunload", capture, { capture: true });
}, []);
}