Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

244
Visualizações
How to understand when a user scrolls to the bottom of a div?

I'm trying to paginate (run an API request) when a user scrolls to the bottom of a div, although it is invoking this multiple times for the same scroll, causing hundreds of API requests for one pagination.

Inside componentDidMount I have,

document.addEventListener('scroll', this.trackScrolling);

Which does this:

trackScrolling = async () => {
    const wrappedElement = document.getElementById('profileArea');

    if (this.isBottom(wrappedElement)) {
        await this.paginateProfiles();
    }
};

isBottom:

isBottom(el : any) {
    return el.getBoundingClientRect().bottom <= window.innerHeight;
}

I will also share paginateProfiles:

async paginateProfiles() {
    if (this.state.profilesPaginating) {
        return;
    }

    await this.setState({
        profilesPaginating: true
    });

    var component = this;
    var page = this.state.profilesNextPage;

    await axios.get(process.env.REACT_APP_API_URL + '/profiles?amount=8&page=' + page)
        .then(async (response) => {
            await component.setState({
                profiles: this.state.profiles.concat(response.data),
                profilesNextPage: this.state.profilesNextPage + 1,
                profilesPaginating: false,
            });
        });
}

Technical description of the issue: This is difficult to explain or what causes it, but it seems to be after around 10 paginations it will suddenly go into spam mode and send about 50 API requests and get stuck on loading. If I scroll back up to the top they will stop after a few seconds and wont make any more.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Explanation

None of the solutions explain they rely on the height of the window always changing after each event listener. If your API returns no results (maybe it ran out) the event handler will think its still initially at the bottom as it hasn't changed, the event listener has been re-registered because the profiles have been fetched but none returned.

Solution

Compare result lengths, and only add the event listener back if it != (there were new records on the last pagination, so there may be more).

Not ideal as it renders the event listener lost if even one API request returns nothing, but it works...

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda