Most people meet IntersectionObserver through lazy-loaded images, and stop there. But the question it answers, "is this element on screen, or close to it?", shows up all over a frontend: infinite lists, analytics, navigation, sticky UI, media.
I built six of these patterns from scratch in React, with tests, and each one had its own trap. This post is the result: one small hook, six patterns, the bug that bit me in each, and how to test all of it without a browser. These are also some of the most common live-coding tasks for frontend roles, so they're worth knowing cold.
To be honest, in the SaaS products I've worked on I never needed IntersectionObserver beyond the basics, so these patterns were a gap for me. I wrote this post to close it and share what I learned.
Why not a scroll listener?
The old way was window.addEventListener('scroll', ...) plus getBoundingClientRect() on every element you care about. That runs on the main thread on every scroll event and forces layout each time you read a rect.…