main
Last change
on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago |
Update repo after prototype presentation
|
-
Property mode
set to
100644
|
File size:
797 bytes
|
Line | |
---|
1 | import { useCallback, useEffect } from 'react';
|
---|
2 | import { off, on } from './misc/util';
|
---|
3 | var useBeforeUnload = function (enabled, message) {
|
---|
4 | if (enabled === void 0) { enabled = true; }
|
---|
5 | var handler = useCallback(function (event) {
|
---|
6 | var finalEnabled = typeof enabled === 'function' ? enabled() : true;
|
---|
7 | if (!finalEnabled) {
|
---|
8 | return;
|
---|
9 | }
|
---|
10 | event.preventDefault();
|
---|
11 | if (message) {
|
---|
12 | event.returnValue = message;
|
---|
13 | }
|
---|
14 | return message;
|
---|
15 | }, [enabled, message]);
|
---|
16 | useEffect(function () {
|
---|
17 | if (!enabled) {
|
---|
18 | return;
|
---|
19 | }
|
---|
20 | on(window, 'beforeunload', handler);
|
---|
21 | return function () { return off(window, 'beforeunload', handler); };
|
---|
22 | }, [enabled, handler]);
|
---|
23 | };
|
---|
24 | export default useBeforeUnload;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.