@jsnooks/use-prevent-leave
v1.0.0
Published
React Hook to prompt the user for confirmation before leaving the page. Useful when changes haven't been saved.
Downloads
1
Maintainers
Readme
usePreventLeave
- protect 버튼 누를 시 윈도우 창을 끄기 전 확인 창이 출력
- unprotect 버튼 누를 시 해당 이벤트 취소
[실습예제]
import React from "react";
const usePreventLeave = () => {
const listener = (e) => {
e.preventDefault();
e.returnValue = "";
};
const enablePrevent = () => window.addEventListener("beforeunload", listener);
const disablePrevent = () =>
window.removeEventListener("beforeunload", listener);
return { enablePrevent, disablePrevent };
};
const App = () => {
const { enablePrevent, disablePrevent } = usePreventLeave();
return (
<div className="App">
<button onClick={enablePrevent}>Protect</button>
<button onClick={disablePrevent}>Unprotect</button>
</div>
);
};
export default App;