svelte-intersection-observer icon indicating copy to clipboard operation
svelte-intersection-observer copied to clipboard

Example with each loop

Open mattpilott opened this issue 3 years ago • 3 comments

Would be handy to see how this works in an each loop in the examples

mattpilott avatar Oct 27 '22 10:10 mattpilott

@mattpilott I did something like this

<script>
  import { onMount } from "svelte"
  import IntersectionObserver from 'svelte-intersection-observer'

  let notifications = []
  let notificationsDom = []

  onMount(() => {
    setInterval(loadNotifications, 30000);
    loadNotifications()  
  })

  async function loadNotifications() {
    const response = await fetch('/api/notifications')
    notifications = (await response.json())
    notificationsDom = new Array(notifications.length)
  }

  async function markReadNotificationId(id) {
    console.log(id)
  }
</script>
<div class="list-group list-group-flush list-group-activity my-n3">
  {#each notifications as notification, i}
    <IntersectionObserver element={notificationsDom[i]} on:intersect={() => {
      markReadNotificationId(notification.id)
    }}>
      <a class="list-group-item text-reset" bind:this={notificationsDom[i]} href="{notification.link}">
        ...
      </a>
    </IntersectionObserver>
  {/each}
</div>

infinito84 avatar Nov 09 '24 16:11 infinito84

Doesn't this create an IntersectionObserver for every element? IntersectionObservers are designed to be created once for the root element.

the-leonat avatar Feb 26 '25 00:02 the-leonat

@metonym hi, I could not make it work with each loop, can you give use example?

harryqt avatar Oct 11 '25 10:10 harryqt