DEV Community

Discussion on: Lazy Loading Images in Svelte

Collapse
 
igorfilippov3 profile image
Ihor Filippov • Edited

Hello. Thank You for this article. It was very helpful for me.
I want to add something. In current situation, we have an IntersectionObserver which never ends observing until node will be destroyed. But we need only one intersection. So I propose to edit your code a little bit.

function onIntersect(entries, observer) {
    if (!src && entries[0].isIntersecting) {
      observer.unobserve(entries[0].target);
      src = item;
    }
  }

We also can pass an observer through parameters props. Thanks to this, a lazyLoad action could be reused in many places.