I also had the same problem. You have to put a wrapper for the component and set the overflow equal to hidden, then set the height of the component equal to the height of the wrapper.
/* Create a new style sheet or add the following code to your existing style sheet */
.wrapper {
overflow: hidden; /* Hide any overflow beyond the content's size */
height: 100vh; /* Set the height equal to the viewport height to limit scrolling */
}
import React, { useState, useEffect, useRef } from 'react';
import axios from 'axios';
import InfiniteScroll from 'react-infinite-scroll-component';
import FeedCard from './FeedCard'; // Assuming this component exists properly
const YourComponent = () => {
const [hasMore, setHasMore] = useState(true);
const [pageNumber, setPageNumber] = useState(1);
const [posts, setPosts] = useState([]);
const wrapperRef = useRef(null); // Use useRef to access the wrapper's height
const fetchDataOnScroll = async () => {
try {
const res = await axios.get(
`${process.env.REACT_APP_API}/api/v1/posts?page=${pageNumber}`
);
if (res.data.doc.length === 0) setHasMore(false);
setPosts((prev) => [...prev, ...res?.data?.doc]);
setPageNumber((prev) => prev + 1);
} catch (error) {
alert('Error fetching Posts');
console.log('ERROR', error.data.response);
}
};
useEffect(() => {
// Call the initial data fetch as well.
fetchDataOnScroll();
}, []);
useEffect(() => {
// Set InfiniteScroll height equal to wrapper's height
if (wrapperRef.current) {
const wrapperHeight = wrapperRef.current.clientHeight;
// Pass the height to InfiniteScroll
// This ensures both components have the same height.
setInfiniteScrollHeight(wrapperHeight);
}
}, [posts]); // Execute this useEffect whenever the number of posts changes
const setInfiniteScrollHeight = (height) => {
// Access InfiniteScroll using ref and set the height
const infiniteScroll = document.querySelector('.infinite-scroll-component');
if (infiniteScroll) {
infiniteScroll.style.height = `${height}px`;
}
};
return (
<div ref={wrapperRef} className="wrapper">
<InfiniteScroll
dataLength={posts.length}
next={fetchDataOnScroll}
hasMore={hasMore}
endMessage="No More Post"
loader={<h1>LOADING...</h1>}
>
{posts.map((post) => (
<FeedCard key={post._id} post={post} />
))}
</InfiniteScroll>
</div>
);
};
export default YourComponent;