@jsnooks/use-before-leave
v1.0.1
Published
React Hook to execute a function when the mouse leaves the page. Useful to show a popup or for analytics.
Downloads
2
Maintainers
Readme
useBeforeLeave
- 마우스가 위로 올라가서 페이지를 벗어날 때
onBefore
실행
[실습예제]
import React, { useEffect } from "react";
const useBeforeLeave = (onBefore) => {
if (typeof onBefore !== "function") return;
const handle = (e) => {
const { clientY } = e;
if (clientY <= 0) {
onBefore();
}
};
useEffect(() => {
document.addEventListener("mouseleave", handle);
return () => document.removeEventListener("mouseleave", handle);
}, []);
};
const App = () => {
const begForLife = () => console.log("Please don't leave");
useBeforeLeave(begForLife);
return (
<div className="App">
<h1>Hello</h1>
</div>
);
};
export default App;