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;
'WEB > Front-End๐' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
React project ์ค ๋์ง๋์ง ์ค๋ฅ ๋ชจ์(+React๋ ์๊ด์๋ ๊ฒ๋) (0) | 2022.01.06 |
---|---|
[React+Firebase] Set up, Auth (0) | 2022.01.04 |
[React Hooks] useFadeln, useNetwork, useScroll (0) | 2021.12.19 |
[React Hooks] useConfirm, preventLeave, useBeforeLeave (0) | 2021.12.19 |
[React Hooks] useEffect, useTitle, useClick (0) | 2021.12.18 |