๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
WEB/Front-End๐ŸŒ

[React Hooks] useFullscreen, useNotification, useAxios

by narang111 2021. 12. 20.

useFullscreen

- fullscreen ์„ ํ•  ๋•Œ ์‚ฌ์ง„์„ ํฌํ•จํ•œ div๊ฐ€ ์ปค์ง„๋‹ค.

- ์ „์ฒดํ™”๋ฉด์—์„œ exit fullscreen์„ ๋ˆ„๋ฅด๋ฉด ๋‹ค์‹œ ์›๋ž˜ ํ™”๋ฉด์œผ๋กœ ๋Œ์•„์˜จ๋‹ค.

import "./styles.css";
import React, { useState, useEffect, useRef } from "react";

const useFullscreen = () => {
  const element = useRef();
  const triggerFull = () => {
    if (element.current) {
      element.current.requestFullscreen();
    }
  };
  const exitFull = () => {
    document.exitFullscreen();
  };
  return { element, triggerFull, exitFull };
};

export default function App() {
  const { element, triggerFull, exitFull } = useFullscreen();
  return (
    <div className="App" style={{ height: "1000vh" }}>
      <div ref={element}>
        <img src="https://www.readersnews.com/news/photo/201912/96670_61911_3434.jpg" />
        <button onClick={exitFull}>Exit fullscreen</button>
      </div>
      <button onClick={triggerFull}>Make fullscreen</button>
    </div>
  );
}

 

์ด๊ฑด callback์„ passing ํ•˜๋Š” ๊ฑฐ

import "./styles.css";
import React, { useState, useEffect, useRef } from "react";

const useFullscreen = (callback) => {
  const element = useRef();
  const triggerFull = () => {
    if (element.current) {
      element.current.requestFullscreen();
      if (callback && typeof callback === "function") {
        callback(true);
      }
    }
  };
  const exitFull = () => {
    document.exitFullscreen();
    if (callback && typeof callback === "function") {
      callback(false);
    }
  };
  return { element, triggerFull, exitFull };
};

export default function App() {
  const onFullS = (isFull) => {
    console.log(isFull ? "we are full" : "we are small");
  };
  const { element, triggerFull, exitFull } = useFullscreen(onFullS);
  return (
    <div className="App" style={{ height: "1000vh" }}>
      <div ref={element}>
        <img src="https://www.readersnews.com/news/photo/201912/96670_61911_3434.jpg" />
        <button onClick={exitFull}>Exit fullscreen</button>
      </div>
      <button onClick={triggerFull}>Make fullscreen</button>
    </div>
  );
}

 

useNotication

https://developer.mozilla.org/ko/docs/Web/API/notification

 

Notification - Web API | MDN

Notifications API์˜ Notification ์ธํ„ฐํŽ˜์ด์Šค๋Š” ์‚ฌ์šฉ์ž์—๊ฒŒ ๋ฐ์Šคํฌํ†ฑ ์•Œ๋ฆผ์„ ์„ค์ •ํ•˜๊ณ  ๋ณด์—ฌ์ฃผ๋Š”๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

developer.mozilla.org

import "./styles.css";
import React, { useState, useEffect, useRef } from "react";

const useNotification = (title, options) => {
  if (!("Notification") in window) {
    return;
  }
  const fireNotif = () => {
    if (Notification.permission !== "granted") {
      Notification.requestPermission().then((permission) => {
        if (permission === "grangted") {
          new Notification(title, options);
        } else {
          return;
        }
      });
    } else {
      new Notification(title, options);
    }
  };
  return fireNotif;
};

export default function App() {
  const triggerNotif = useNotification("wing wing", {body:"bell is ringing"});
  return (
    <div className="App" style={{ height: "1000vh" }}>
      <button onClick={triggerNotif}>Hello</button>
    </div>
  );
}

 

 

useAxios

๐Ÿ”ฝ App.js

import "./styles.css";
import useAxios from "./useAxios";
import React, { useState, useEffect, useRef } from "react";

export default function App() {
  const { loading, data, error, refetch } = useAxios({
    url: "https://yts.mx/api/v2/list_movies.json"
  });
  console.log(`Loading:${loading}\nError:${error}\nData:${data}`);
  return (
    <div className="App" style={{ height: "1000vh" }}>
      <h1>{data && data.status}</h1>
      <h2>{loading && "Loading"}</h2>
      <button onClick={refetch}>Refetch</button>
    </div>
  );
}

 

๐Ÿ”ฝ useAxios.js

import defaultAxios from "axios";
import { useEffect, useState } from "react";

const useAxios = (opts, axiosInstance = defaultAxios) => {
  const [state, setState] = useState({
    loading: true,
    error: null,
    data: null
  });
  const [trigger, setTrigger] = useState(0);
  if (!opts.url) {
    return;
  }
  const refetch = () => {
    setState({
      ...state,
      loading: true
    });
    setTrigger(Date.now());
  };
  useEffect(() => {
    axiosInstance(opts)
      .then((data) => {
        setState({
          ...state,
          loading: false,
          data
        });
      })
      .catch((error) => {
        setState({ ...state, lading: false, error });
      });
  }, [trigger]);
  return { ...state, refetch };
};

export default useAxios;