๐ด Introduction
โช Window Notification์ ๋ํ ์ต์ ์ ์ค์ ํ๋ฉด ๊ถํ ํ์ฉ ์ฌ๋ถ ์ดํ Notification์ ๋์์ฃผ๋ Hook
๐ Hook
export const useNotification = (title, options) => {
if (!("Notification" in window)) {
return;
}
const fireNotif = () => {
if (Notification.permission !== "granted") {
Notification.requestPermission().then((permission) => {
if (permission === "granted") {
new Notification(title, options);
} else {
return;
}
});
} else {
new Notification(title, options);
}
};
return fireNotif;
};
๐ก Usage
import { useNotification } from "./hooks/useNotification";
function App() {
const triggerNotif = useNotification("Notification Title", {
body: "Notification Body",
});
return (
<div className="App">
<button onClick={triggerNotif}>Hello</button>
</div>
);
}
export default App;
'React > Hook' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ Hook ] useScroll (0) | 2022.08.10 |
---|---|
[ Hook ] usePreventLeave (0) | 2022.08.10 |
[ Hook ] useHover (0) | 2022.08.10 |
[ Hook ] useConfirm (0) | 2022.08.10 |
[ Hook ] useBeforeLeave (0) | 2022.08.10 |