I want to implement dynamic pagination with intersection observer API and it works as intended: scrolling down the page, new data is fetched but after that the page is scrolled to its beginning. I know that it happens because react re-renders the page after loading new info. But can I somehow prevent this?
Here is my code:
function AllUniversititesList() {
const [loading, setLoading] = useState(false);
const [listOfUnis, setListOfUnis] = useState([]);
const [isSearched, setSearched] = useState(false);
const lastElement = useRef();
const observer = useRef();
async function fetchAllUnis() {
setLoading(true);
const result = await SearchService.getAllUniversities(); //return of data from API
setListOfUnis([...listOfUnis, ...result]);
setLoading(false);
}
useEffect(() => {
if(!listOfUnis.length) {
fetchAllUnis();
}
}, [])
useEffect(() => {
if(loading) return;
if(observer.current) observer.current.disconnect();
const callback = (entries, observer) => {
if(entries[0].isIntersecting) {
fetchAllUnis()
}
}
observer.current = new IntersectionObserver(callback);
observer.current.observe(lastElement.current);
}, [loading])
return (
<section className='min-h-screen bg-slate-200 pt-[7%] '>
{!loading && listOfUnis && listOfUnis.length > 0 && <UniversitiesList info = {listOfUnis}/>}
<div className='w-full h-4' ref={lastElement}></div>
</section>
)
}