fix: resume InView observation when triggerOnce is disabled - #782
Conversation
|
|
|
@ddussi is attempting to deploy a commit to the Daniel Schmidt's projects Team on Vercel. A member of the Team first needs to authorize it. |
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
Thanks, this is a nicely focused fix and the reported true → false case makes sense. One small edge case may be worth covering before merging: adding triggerOnce to the general reinitialization condition recreates the observer in both directions. A fresh observer reports the element’s current state, so toggling triggerOnce while it is already in view could repeat onChange(true) and, for false → true, immediately stop observing again. Could you add coverage for changing it in both directions while the element is already visible, so we can confirm the intended behavior? |
Hi! I put together a small fix for the lifecycle behavior reported in #781,
along with a regression test.
Summary
<InView>whentriggerOncechangesfrom
truetofalseWhy
After the first intersecting entry,
handleChange()invokes and clears thestored
unobservecallback whentriggerOnceis enabled.componentDidUpdate()already reinitializes observation when severalobserver-related props change, but
triggerOncewas not included in thatcondition. As a result, changing only
triggerOncetofalseleaves themounted node unobserved until another relevant prop changes or the component is
remounted.
This change adds
triggerOnceto the existing reinitialization condition. Itdoes not introduce a new API or a generic resubscribe mechanism.
Fixes #781
Verification
CI=true pnpm test— 7 files, 97 tests passedCI=true pnpm --filter react-intersection-observer exec vitest --coverage—97 tests passed
pnpm --filter react-intersection-observer typecheckpnpm biome ci .pnpm fallow audit --base upstream/main --coverage packages/react-intersection-observer/coverage/coverage-final.jsonpnpm build:allIf runtime updates to
triggerOnceare not intended for the class-based API, orif you would prefer a different lifecycle behavior, I’d be happy to adjust the
approach. Thanks for taking a look!