Intersection Observer & Efficient Scroll Handling

What is the best way to implement scroll-based infinite image galleries?
const galleryObserver = new IntersectionObserver((entries) => {
  entries.forEach(entry => {
    if (entry.isIntersecting && !isLoading) {
      isLoading = true;
      loadMoreImages()
        .then(images => {
          appendImages(images);
          isLoading = false;
        })
        .catch(error => {
          console.error('Failed to load images:', error);
          isLoading = false;
        });
    }
  });
}, {
  root: null,
  rootMargin: '200px 0px',
  threshold: 0
});
Next Question (18/20)