Download react-use/src/useBeforeUnload.ts from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 752 Bytes
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-use/src/useBeforeUnload.ts
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset@1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-use/src/useBeforeUnload.ts
-
curl -L -o useBeforeUnload.ts https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-use/src/useBeforeUnload.ts
752 Bytes
| import { useCallback, useEffect } from 'react'; | |
| import { off, on } from './misc/util'; | |
| const useBeforeUnload = (enabled: boolean | (() => boolean) = true, message?: string) => { | |
| const handler = useCallback( | |
| (event: BeforeUnloadEvent) => { | |
| const finalEnabled = typeof enabled === 'function' ? enabled() : true; | |
| if (!finalEnabled) { | |
| return; | |
| } | |
| event.preventDefault(); | |
| if (message) { | |
| event.returnValue = message; | |
| } | |
| return message; | |
| }, | |
| [enabled, message] | |
| ); | |
| useEffect(() => { | |
| if (!enabled) { | |
| return; | |
| } | |
| on(window, 'beforeunload', handler); | |
| return () => off(window, 'beforeunload', handler); | |
| }, [enabled, handler]); | |
| }; | |
| export default useBeforeUnload; | |