question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Infinite scroll not working

See original GitHub issue

Describe the bug

I’m trying to use infinite scroll, however the provided examples don’t seem to work.

<template>
  <div
    class="flex flex-col gap-2 p-4 w-300px h-300px m-auto overflow-y-scroll bg-gray-500/5 rounded"
    ref="el"
  >
    <div v-for="item in data" :key="item">
      {{ item }}
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from "vue";
import { useInfiniteScroll } from "@vueuse/core";

const el = ref<HTMLElement | null>(null);

useInfiniteScroll(el, () => console.log("fetch"), { distance: 100 }); // nothing logged to console
</script>

Reproduction

https://stackblitz.com/edit/vitejs-vite-srqqu3

System Info

System:
    OS: Linux 5.18 Arch Linux
    CPU: (12) x64 AMD Ryzen 5 3600 6-Core Processor
    Memory: 19.16 GB / 31.27 GB
    Container: Yes
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 18.3.0 - /usr/bin/node
    Yarn: 1.22.19 - /usr/bin/yarn
    npm: 8.5.5 - /usr/bin/npm
  npmPackages:
    @vueuse/components: ^8.6.0 => 8.6.0 
    @vueuse/core: ^8.6.0 => 8.6.0 
    vue: ^3.2.36 => 3.2.36

Used Package Manager

npm

Validations

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
adgowercommented, Jul 15, 2022

I accomplished this using ‘useIntersectionObserver’

<script setup>
import { ref } from 'vue';
import { useIntersectionObserver } from '@vueuse/core';

const el = ref(null);
const data = ref([1, 2, 3, 4, 5, 6]);

useIntersectionObserver(
  el,
  () => {
    const length = data.value.length + 1
    data.value.push(...Array.from({ length: 5 }, (_, i) => length + i))
  },
  {
    threshold: 0.5,
  }
)
</script>

<template>
  <a-row :gutter="[16, 16]">
    <a-col :span="24" v-for="item in data" :key="item">
      <a-card title="Card title">
        <p>card content {{ item }}</p>
      </a-card>
    </a-col>
  </a-row>
  <span ref="el"></span>
</template>
1reaction
lrstanleycommented, Jun 23, 2022

My hacky solution for now is once I fetch data, I check if the container still isn’t scrollable, it’ll make another call (assuming there is still more data to fetch), e.g:

    // if this fetch was triggered by a scroll event, don't trigger it a second time.
    if (wasScrollEvent) return
    setTimeout(() => {
      if (scrollContainer.value.scrollHeight <= scrollContainer.value.clientHeight) {
        fetchEvents(true)
      }
    }, 500)

Full logic:

const scrollContainer = ref(null) // attached to infinite scroll container.

function fetchEvents(wasScrollEvent) {
  if (!hasNextPage.value) return

  events.executeQuery().then((result) => {
    const data = result.data.value.githubevents

    if (data.pageInfo.hasNextPage) {
      cursor.value = data.pageInfo.endCursor
    } else {
      hasNextPage.value = false
    }

    fetched.value = [...fetched.value, ...data.edges.map(({ node }) => toRaw(node))]

    // if this fetch was triggered by a scroll event, don't trigger it a second time.
    if (wasScrollEvent) return
    setTimeout(() => {
      if (scrollContainer.value.scrollHeight <= scrollContainer.value.clientHeight) {
        fetchEvents(true)
      }
    }, 500)
  })
}

onMounted(() => {
  fetchEvents()
})
Read more comments on GitHub >

github_iconTop Results From Across the Web

ngInfiniteScroll not working - Stack Overflow
Scroll event is only triggered on page load, after that nothing seems to trigger it. Can anyone please shed some light. I tried...
Read more >
Infinite scroll stopped working - WordPress.org
Hello,. Since yesterday the infinite scroll option has stopped working. The website went live over 1 month ago and it had been working...
Read more >
Issues for Views Infinite Scroll | Drupal.org
Title Status Priority Cate... Allow setting scroll threshold and delay Reviewed & tested by the community Normal Featu... Parametrize scrollThreshold field Needs review Normal Featu... AJAX...
Read more >
Infinite scroll not working - Get Help - Frontity Community Forum
It is when the user scrolls down, the infinite scroll triggers, he/she clicks on a post and finally goes back to the last...
Read more >
I did pagination but infinite scroll not working? - ProcessWire
Hi, i implemented pagination for my site : http://www.diana-homes.com Now i want infinite scroll for home page. I did all the things ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found