π΄ Introduction
βͺ νμ¬ μ€ν¬λ‘€μ y μ’ν κ°μ ꡬν΄μ 리ν΄ν΄μ£Όλ Hook
π Hook

import { useState, useEffect } from "react";
export 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;
};
π‘ Usage

import styled from "styled-components";
import { useScroll } from "./hooks/useScroll";
function App() {
const { y } = useScroll();
return <Box style={{ backgroundColor: y > 100 ? "red" : "blue" }}></Box>;
}
const Box = styled.div`
height: 200vh;
`;
export default App;
'React > Hook' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[ Hook ] usePreventLeave (0) | 2022.08.10 |
---|---|
[ Hook ] useNotification (0) | 2022.08.10 |
[ Hook ] useHover (0) | 2022.08.10 |
[ Hook ] useConfirm (0) | 2022.08.10 |
[ Hook ] useBeforeLeave (0) | 2022.08.10 |