![[CODE] useResFetchCount [9/6 study]](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdna%2FE1Olx%2FbtsJtyTHfLe%2FAAAAAAAAAAAAAAAAAAAAAKZ2fUpeRapkdQQ0mZJX90q0fysZe64amAeFqLfI2m43%2Fimg.png%3Fcredential%3DyqXZFxpELC7KVnFOS48ylbz2pIh7yKj8%26expires%3D1756652399%26allow_ip%3D%26allow_referer%3D%26signature%3DVXfVPlyUacRXNbFyjECQxut68MY%253D)
[CODE] useResFetchCount [9/6 study]Frontend/CODE2024. 9. 6. 16:36
Table of Contents
useResFetchCount - 스크린 사이즈에 맞춰서 데이터를 다르게 불러와야해서 작성한 코드입니다
프로젝트 하면서 작성한 코드 기록하는 게시물입니다.
사용법
const fetchSize = useResFetchCount({ mobileSize: 4, tabletSize: 9, desktopSize: 8 });
import { useEffect, useState } from 'react';
interface FetchSizeOptions {
desktopSize: number;
mobileSize: number;
tabletSize: number;
}
const useResFetchCount = ({ mobileSize, tabletSize, desktopSize }: FetchSizeOptions) => {
const [fetchSize, setFetchSize] = useState<number>(9);
useEffect(() => {
const updateFetchSize = () => {
if (window.innerWidth < 424) {
setFetchSize(mobileSize);
} else if (window.innerWidth < 768) {
setFetchSize(tabletSize);
} else {
setFetchSize(desktopSize);
}
};
updateFetchSize();
window.addEventListener('resize', updateFetchSize);
return () => {
window.removeEventListener('resize', updateFetchSize);
};
}, [mobileSize, tabletSize, desktopSize]);
return fetchSize;
};
export default useResFetchCount;
@동혁이 :: Eun_Frontend
프론트엔드 공부일지 입니다.
포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!