[React JS] #Hooks 3
# USEEFFECT
6. useScroll
const useScroll = () => {
const [state, setState] = useState({
x: 0,
y: 0
});
const onscroll = () => {
setState({ y: window.scrollY, x: window.scrollX });
};
useEffect(() => {
window.addEventListener("scroll", onscroll);
return () => window.removeEventListener("scroll", onscroll);
}, []);
return state;
};
const App = () => {
const { y } = useScroll();
return (
<div style={{ height: "1000vh" }}>
<h1 style={{ position: "fixed", color: y > 100 ? "red" : "blue" }}>
Hello!
</h1>
</div>
);
};
7. useFullscreen
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 };
};
const App = () => {
const onFulls = (isFull) => {
console.log(isFull ? "We are full" : "We are small");
};
const { element, triggerFull, exitFull } = useFullscreen(onFulls);
return (
<div style={{ height: "1000vh" }}>
<div ref={element}>
<img src="http://image.newsis.com/2021/05/03/NISI20210503_0017412357_web.jpg" />
<button onClick={triggerFull}>Make fullscreen</button>
<button onClick={exitFull}>exit fullscreen</button>
</div>
</div>
);
};
8. useNotification
useAxios.js
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, loading: false, error });
});
}, [trigger]);
return { ...state, refetch };
};
Index.js
const App = () => {
const { loading, data, refetch } = useAxios({
url: "https://yts.am/api/v2/list_movies.json"
});
return (
<div>
<h1>{data && data.status}</h1>
<h1>{loading && "Loading"}</h1>
<button onClick={refetch}>refetch</button>
</div>
);
};
Author And Source
이 문제에 관하여([React JS] #Hooks 3), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://velog.io/@choiish98/React-JS-Hooks-4
저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
const useScroll = () => {
const [state, setState] = useState({
x: 0,
y: 0
});
const onscroll = () => {
setState({ y: window.scrollY, x: window.scrollX });
};
useEffect(() => {
window.addEventListener("scroll", onscroll);
return () => window.removeEventListener("scroll", onscroll);
}, []);
return state;
};
const App = () => {
const { y } = useScroll();
return (
<div style={{ height: "1000vh" }}>
<h1 style={{ position: "fixed", color: y > 100 ? "red" : "blue" }}>
Hello!
</h1>
</div>
);
};
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 };
};
const App = () => {
const onFulls = (isFull) => {
console.log(isFull ? "We are full" : "We are small");
};
const { element, triggerFull, exitFull } = useFullscreen(onFulls);
return (
<div style={{ height: "1000vh" }}>
<div ref={element}>
<img src="http://image.newsis.com/2021/05/03/NISI20210503_0017412357_web.jpg" />
<button onClick={triggerFull}>Make fullscreen</button>
<button onClick={exitFull}>exit fullscreen</button>
</div>
</div>
);
};
useAxios.js
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, loading: false, error });
});
}, [trigger]);
return { ...state, refetch };
};
Index.js
const App = () => {
const { loading, data, refetch } = useAxios({
url: "https://yts.am/api/v2/list_movies.json"
});
return (
<div>
<h1>{data && data.status}</h1>
<h1>{loading && "Loading"}</h1>
<button onClick={refetch}>refetch</button>
</div>
);
};
Author And Source
이 문제에 관하여([React JS] #Hooks 3), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@choiish98/React-JS-Hooks-4저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)