Closed fabb closed 4 years ago
My current understanding (at least from MDN) is this:
The callback receives as input an array of all of IntersectionObserverEntry objects, one for each threshold which was crossed, and a reference to the IntersectionObserver object itself.
Based off this and since this hook instantiates a new IntersectionObserver
per hook, the only situation where entries
should contain more than one entry is if the hook is provided an array of thresholds for the threshold
option and triggerOnce
is false
.
Please feel free to correct me if this is incorrect though!
That being said, I totally agree with it being simpler and more flexible to just provide the entire entries
array to the callback instead and letting the consumer decide what they ultimately want to do with it.
Hey @fabb, I've opened #7 which now provides the list of entries to the callback instead. If possible, I'd love to get your feedback on it.
Otherwise, if everything looks good, I'll go ahead and publish the update.
Based off this and since this hook instantiates a new IntersectionObserver per hook, the only situation where entries should contain more than one entry is if the hook is provided an array of thresholds for the threshold option and triggerOnce is false.
This is false. I verified it on Chrome with console logs of the entries array. Happens only when scrolling very quickly, or when using an url #fragment
To directly jump down on the page, either in combination with high cpu load. My IntersectionObserver only had 1 threshold. I could not verify it in Firefox though, so MDN might only describe Firefox‘ implementation.
This hook has a major issue in this line:
The browser might defer calling the intersection observer callback when it is busy doing other stuff. In that case, the callback will be called with multiple elements in the
entries
array. That's why it's an array in the first place.Here's a related webkit bug report that pinned down a production problem to this browser behavior: https://bugs.chromium.org/p/chromium/issues/detail?id=941681#c7
In order to fix this, you need to do 2 different behaviors, depending on
triggerOnce
:The behavior with
triggerOnce=false
described by theINFO
comment is debatable, maybe it makes more sense to execute the callback with all elements of theentries
array?