Frontend/React Hook

[React-Hooks] React Hooks - useNotification

갈푸라떼 2022. 7. 14. 00:12

* useNotification

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;