Search code examples
javascriptreactjsinfinite-scroll

How to add loader correctly while infinite scrolling?


I tried multiple ways to implement loading while fetching more data during infinite scrolling, but nothing worked properly, so I deleted loader; I have here state (with redux) named: loading but cannot write the logic of loading correctly. Could you please tell me how I can make it work? Here I will provide with code:

import React, {useEffect} from 'react';
import { Link } from 'react-router-dom';
import { useSelector, useDispatch } from 'react-redux';
import {setAllUsers, setLoading, setPage} from '../redux/actions/actions';
import User from './User';
import '../styles/AllUsersList.css';

const AllUsersList = () => {
    const allUsers = useSelector(state => state.setAllUsersReducer);
    const page = useSelector(state => state.setPageReducer);
    const loading = useSelector(state => state.setLoadingReducer);
    const dispatch = useDispatch();
  

    const fetchAllUsers = () => {
       fetch(`${url}/${page}/15`)
            .then(res => res.json())
            .then(data => {
                dispatch(setAllUsers(data.list));
            })
            .catch(err => console.log('Error message: ', err))
    }

    useEffect(() => {
      fetchAllUsers();
    }, [page])

    const handleScroll = () => {
        dispatch(setPage());
    }

    window.onscroll = function () {
        if(window.innerHeight + document.documentElement.scrollTop === document.documentElement.offsetHeight) {
            handleScroll();
        }
    }


    return (
        <div className="allUsersList">
            {
                allUsers ? (
                    allUsers.map((user, index) => (
                        <Link key={user.id} to={`/user/${user.id}`}>
                            <User name={user.name} lastName={user.lastName} prefix={user.prefix} title={user.title} img={user.imageUrl}/>
                        </Link>
                    ))
                ) : (
                    <div> Loading... </div>
                )
            }
        </div>
    )
}

export default AllUsersList;

Solution

  • Your state loading would be set to true in your function fetchAllUsers the data and when the promise resolves it gets set to false.

    Here's an example on how you would do it, you can adapt it to use a redux dispatcher to change loading state.

    const loading = useState(false);
    
    ...
    
    const fetchAllUsers = () => {
           setLoading(true);
           fetch(`${url}/${page}/15`)
                .then(res => res.json())
                .then(data => {
                    dispatch(setAllUsers(data.list));
                })
                .catch(err => console.log('Error message: ', err))
                .finally(() => {
                    setLoading(false);
                 })
        }
    
    ...
    
    {
                    !loading ? (
                        allUsers.map((user, index) => (
                            <Link key={user.id} to={`/user/${user.id}`}>
                                <User name={user.name} lastName={user.lastName} prefix={user.prefix} title={user.title} img={user.imageUrl}/>
                            </Link>
                        ))
                    ) : (
                        <div> Loading... </div>
                    )
                }