인피니트 스크롤링 (완성)
useEffect(() => {
function onScroll() {
if (
window.pageYOffset + document.documentElement.clientHeight >
document.documentElement.scrollHeight - 300
) {
if (hasMorePost && !loadPostsLoading) {
const lastId = mainPosts[mainPosts.length - 1]?.id;
dispatch({ type: LOAD_POSTS_REQUEST, lastId });
}
}
}
window.addEventListener("scroll", onScroll);
return () => {
window.removeEventListener("scroll", onScroll);
//리턴해서 다시 삭제 안해주면 메모리에 쌓이게 된다.
};
}, [hasMorePost, loadPostsLoading, mainPosts]);Last updated