From 3fe051fb65318d79d12bb19695e3380090da7ab1 Mon Sep 17 00:00:00 2001 From: Jaewook Ahn Date: Sun, 14 Aug 2022 19:49:52 +0900 Subject: [PATCH] Fix address list scroll end detection --- src/App.tsx | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index dc59119..1564852 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -2,7 +2,7 @@ /** * External modules */ -import React, { useCallback, useEffect, useMemo, useState } from "react"; +import React, { useCallback, useEffect, useMemo, useRef, useState } from "react"; import styled from "styled-components"; import { Button, @@ -109,6 +109,7 @@ export const App = (props: Props) => { const [showLegacyAddr, setShowLegacyAddr] = useState(true); const [addressData, setAddressData] = useState([]); const [updatingSettings, setUpdatingSettings] = useState(false); + const contentRef = useRef(null); const handleSearchValueChange = useCallback((e: React.ChangeEvent) => { setSearchValue(e.target.value); @@ -226,9 +227,12 @@ export const App = (props: Props) => { ], [showEngAddr, showRoadAddr, showLegacyAddr, updatingSettings, handleSearchOptionClick]); const handleScrollEvent = useCallback(() => { - const list = document.getElementsByClassName("address-list"); - const bottom = list[0]?.getBoundingClientRect().bottom ?? 0; - if (bottom > 0 && bottom <= window.innerHeight) { + const scrollContainer = document.getElementById("content"); + if (!scrollContainer) { + return; + } + + if (scrollContainer.scrollHeight - scrollContainer.scrollTop === scrollContainer.clientHeight) { console.log("scroll occurred"); loadNextAddress(); } @@ -252,11 +256,12 @@ export const App = (props: Props) => { }, [settings]); useEffect(() => { - document.addEventListener("scroll", handleScrollEvent); + const scrollContainer = contentRef.current; + scrollContainer?.addEventListener("scroll", handleScrollEvent); return () => { - document.removeEventListener("scroll", handleScrollEvent); + scrollContainer?.removeEventListener("scroll", handleScrollEvent); }; - }, [handleScrollEvent]); + }, [contentRef, handleScrollEvent]); return ( @@ -272,7 +277,7 @@ export const App = (props: Props) => { onSearch={handleSearchClick} /> - +