1

Hi I had been try to perform infinite scrolling in React JS but i got problem when try to scroll to next page. The next keyword in infinitescroll will direct call the API multiple times. Below show as my current code.

// States and call API
const [ responseData, setresponseData ] = useState([]);
const [ page, setPage ] = useState(1);
const [ totalPage, setTotalPage ] = useState(1);
const [ totalLength, setTotalLength ] = useState(0);

const fetchData = useCallback(async () => {
    
    await axios.get(`http://sampleURL/api/?page=${ page }`)
    .then((response)=>{
        console.log(response.data.users)
        // setresponseData(prev => [...prev, ...response.data.users.data]);
        setresponseData(responseData.concat(response.data.users.data));
        setTotalLength(response.data.users.total)
        setTotalPage(response.data.users.last_page)
        setPage( page + 1 )
    })

}, [ page, responseData ]);

useEffect(()=>{
    fetchData()
}, [fetchData])


// Return data
return (
    <div className=' w-5/6 mx-auto mt-10 h-full overflow-auto' id="scrollableDiv">
        
        <InfiniteScroll 
            dataLength={ totalLength } 
            next={ fetchData } 
            hasMore={ true }
            scrollableTarget="scrollableDiv"
        >
            <ResponseList responseData={responseData}/>
        </InfiniteScroll>

        <div className=' fixed w-12 h-12 bg-black bottom-44 right-0 rounded-tl-lg rounded-bl-lg text-white flex items-center justify-center'>
            <FaSearch className='text-2xl' />
        </div>

        <div className=' fixed w-12 h-12 bg-black bottom-28 right-0 rounded-tl-lg rounded-bl-lg text-white flex items-center justify-center'>
            <FaFilter className='text-2xl' />
        </div>

        <div className=' fixed w-12 h-24 bg-black bottom-0 right-0 rounded-tl-lg text-white flex flex-col text-center text-md items-center justify-center'>
            <span>{ page }</span>
            <span>----</span>
            <span>{ totalPage }</span>
        </div>
    </div>
)

Hope you guys can help with this and I'm not sure where did I done wrong.

Dirty Rex
  • 13
  • 4
  • The issue you're experiencing is likely caused by the fact that you're updating the page state variable in the **fetchData** function, but you're also using it as a dependency for the **useEffect** hook. This creates a loop where the **fetchData** function is called multiple times in a row, causing the API to be called multiple times as well. – Lalit Tyagi Jan 19 '23 at 06:02
  • @LalitTyagi Oh i see, because I try to use it at the beginning to show first page data but currently I did removed the dependency of `fetchData` and now it show only the first page – Dirty Rex Jan 19 '23 at 06:06

0 Answers0