[강의노트] [React] useNotification

Sejeong Yang·2021년 3월 15일
0

React

목록 보기
7/12
post-custom-banner
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;
};

const App = () => {
    const triggerNotif = useNotification("Click Button", {
      body: "Click me again!"
    });
    return (
    <div className="App" style={{height: "70vw" }}>
      <button onClick={triggerNotif}>Hello!</button>
    </div>
  );
};
profile
Front End Developer
post-custom-banner

0개의 댓글