Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(demo): lazy render example tiles #1896

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

williaster
Copy link
Collaborator

🚀 Enhancements

Improve the performance of the initial gallery page load by lazy rendering visible tiles

Before After
slower faster

observer.unobserve(curr);
}
};
}, [ref]);
Copy link
Member

@hshoff hshoff Dec 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, might not want to use a ref as an effect dep. It'll be stable across re-renders and not trigger the effect. Can use useCallback(). https://legacy.reactjs.org/docs/hooks-faq.html#how-can-i-measure-a-dom-node

}, []);

return { everVisible, ref };
}
Copy link
Member

@indiesquidge indiesquidge Dec 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about this hook instead?

function useIntersectionObserver() {
  const [entry, setEntry] = useState<IntersectionObserverEntry | null>(null);
  const prevObserver = useRef<IntersectionObserver | null>(null);

  const ref = useCallback((node: Element) => {
    if (prevObserver.current) {
      prevObserver.current.disconnect();
      prevObserver.current = null;
    }

    const observer = new IntersectionObserver(
      ([entry]) => {
        setEntry(entry);
      },
      { threshold: 0.01, root: null },
    );

    observer.observe(node);
    prevObserver.current = observer;
  }, []);

  return [ref, entry];
}

This could be a better approach for a few reasons:

  1. I think useIntersectionObserver as a name feels like it more directly reflects its functionality, while useEverVisible is more abstract.
  2. I think using disconnect is better because it completely removes the observer and stops watching all elements, ensuring proper cleanup, whereas unobserve only stops watching a specific element without cleaning up the observer itself, which can lead to memory leaks if not handled properly.
  3. We can return the full IntersectionObserverEntry, giving access to more useful visibility data (maybe you don't want or care about this though since we're only using it to check if an element is visible or not).
  4. We are only observing one element at a time in the hook, so even though the callback of the IntersectionObserver always returns an array of entries, the array will always have just one entry, so we don't need to loop over every entry.
  5. The hook has fewer concerns about state tracking and effects.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dig it! this is a great suggestion for all the reasons you listed, I was just being lazy (hah!) :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants