mirror of
https://github.com/streamich/react-use.git
synced 2026-01-18 14:06:52 +00:00
Refactor useBeforeUnload with window.addEventListener
Remove message param from useBeforeUnload examples.
This commit is contained in:
parent
a428a15eb9
commit
7653597a37
@ -9,7 +9,7 @@ React side-effect hook that shows browser alert when user try to reload or close
|
||||
import {useBeforeUnload} from 'react-use';
|
||||
|
||||
const Demo = () => {
|
||||
useBeforeUnload('Are you sure?');
|
||||
useBeforeUnload();
|
||||
|
||||
return null;
|
||||
};
|
||||
|
||||
@ -4,7 +4,7 @@ import {useBeforeUnload} from '..';
|
||||
import ShowDocs from './util/ShowDocs';
|
||||
|
||||
const Demo = () => {
|
||||
useBeforeUnload('Are you sure?');
|
||||
useBeforeUnload();
|
||||
|
||||
return (
|
||||
<div>
|
||||
|
||||
@ -2,15 +2,14 @@ import {useEffect} from "react";
|
||||
|
||||
const useBeforeUnload = (message?: string) => {
|
||||
useEffect(() => {
|
||||
window.onbeforeunload = e => {
|
||||
e.returnValue = message;
|
||||
return message;
|
||||
const beforeUnload = (e) => {
|
||||
e.preventDefault();
|
||||
e.returnValue = message || "";
|
||||
};
|
||||
|
||||
return () => {
|
||||
window.onbeforeunload = null;
|
||||
return;
|
||||
}
|
||||
window.addEventListener("beforeunload", beforeUnload);
|
||||
|
||||
return () => window.removeEventListener("beforeunload", beforeUnload);
|
||||
}, [message]);
|
||||
};
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user