react-infinite-scroll-component icon indicating copy to clipboard operation
react-infinite-scroll-component copied to clipboard

Scroll up is loading data !!!

Open fox19960910 opened this issue 4 years ago • 6 comments

I'm having problems, if I scroll too fast to the bottom of the page then when scrolled "infinity scroll" trigger called "next "

fox19960910 avatar Apr 13 '21 04:04 fox19960910

I faced the same issue. @fox19960910 did you find a solution?

OstapBO avatar Apr 23 '21 16:04 OstapBO

<InfiniteScroll dataLength={page * 10} pullDownToRefreshThreshold={50} next={loadMoreConversation} scrollableTarget="scrollableDiv" hasMore={true} loader={<h4>Loading...</h4>} > <ChatItemList chatItems={chatItems} /> </InfiniteScroll>

use pullDownToRefreshThreshold={50}. i used this method. page is the length of content

Sooraj-s-98 avatar Jul 23 '21 13:07 Sooraj-s-98

Facing the same issue, even pullDownToRefresh={false} doesn't work

vjtyagi avatar Sep 03 '21 06:09 vjtyagi

`import axios from 'axios'; import { useEffect, useState, useRef } from 'react';

const TOTAL_PAGES = 3;

const App = () => { const [loading, setLoading] = useState(true); const [allUsers, setAllUsers] = useState([]); const [pageNum, setPageNum] = useState(1); const [element, setElement] = useState(null);

const observer = useRef(
	new IntersectionObserver(
		(entries) => {
			const first = entries[0];
			if (first.isIntersecting) {
				setPageNum((no) => no + 1);
			}
		},
		{ threshold: 1 }
	)
);

const callUser = async () => {
	setLoading(true);
	let response = await axios.get(
		`https://randomuser.me/api/?page=${pageNum}&results=25&seed=abc`
	);
	let all = new Set([...allUsers, ...response.data.results]);
	setAllUsers([...all]);
	setLoading(false);
};

useEffect(() => {
	if (pageNum <= TOTAL_PAGES) {
		callUser();
	}
}, [pageNum]);

useEffect(() => {
	const currentElement = element;
	const currentObserver = observer.current;

	if (currentElement) {
		currentObserver.observe(currentElement);
	}

	return () => {
		if (currentElement) {
			currentObserver.unobserve(currentElement);
		}
	};
}, [element]);

const UserCard = ({ data }) => {
	return (
		<div className='p-4 border border-gray-500 rounded bg-white flex items-center'>
			<div>
				<img
					src={data.picture.medium}
					className='w-16 h-16 rounded-full border-2 border-green-600'
					alt='user'
				/>
			</div>

			<div className='ml-3'>
				<p className='text-base font-bold'>
					{data.name.first} {data.name.last}
				</p>
				<p className='text-sm text-gray-800'>
					{data.location.city}, {data.location.country}
				</p>
				<p className='text-sm text-gray-500 break-all'>
					{data.email}
				</p>
			</div>
		</div>
	);
};

return (
	<div className='mx-44 bg-gray-100 p-6'>
		{console.log(pageNum)}
		<h1 className='text-3xl text-center mt-4 mb-10'>All users</h1>

		<div className='grid grid-cols-3 gap-4'>
			{allUsers.length > 0 &&
				allUsers.map((user, i) => {
					if (
						i === allUsers.length - 1 &&
						!loading &&
						pageNum <= TOTAL_PAGES
					) {
						return (
							<div
								key={`${user.name.first}-${i}`}
								ref={setElement}
							>
								<UserCard data={user} />
							</div>
						);
					}
					return (
						<UserCard
							data={user}
							key={`${user.name.first}-${i}`}
						/>
					);
				})}
		</div>
		{loading && <p className='text-center'>loading...</p>}

		{pageNum - 1 === TOTAL_PAGES && (
			<p className='text-center my-10'>♥</p>
		)}
	</div>
);

};

export default App;`

Sooraj-s-98 avatar Sep 04 '21 08:09 Sooraj-s-98

It is better than this package

Sooraj-s-98 avatar Sep 04 '21 09:09 Sooraj-s-98

Use this @OstapBO

Sooraj-s-98 avatar Sep 05 '21 07:09 Sooraj-s-98