Intersection Observer & Efficient Scroll Handling

How should you handle scroll-based infinite loading to prevent performance issues?
const loadMoreCallback = (entries) => {
  const trigger = entries[0];
  if (trigger.isIntersecting && !isLoading) {
    isLoading = true;
    loadNextPage()
      .then(() => {
        isLoading = false;
      })
      .catch(error => {
        console.error('Loading failed:', error);
        isLoading = false;
      });
  }
};

const observer = new IntersectionObserver(loadMoreCallback, {
  root: null,
  rootMargin: '200px'
});
Next Question (9/20)