Frontend/React Hook
[React-Hooks] React Hooks - useNotification
갈푸라떼
2022. 7. 14. 00:12
* useNotification
- useNotification는 알림이 실행되는 hook이다.
- Notification에 대한 정보는 https://developer.mozilla.org/ko/docs/Web/API/notification 해당 링크를 참조
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;
};
const App = () => {
const triggerNotif = useNotification("check", { body: "body" });
return (
<div>
<button onClick={triggerNotif}>Click</button>
</div>
);
};
export default App;