Dark Mode

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

prevent callback skipping with triggerOnce and merged refs#747

Merged
thebuilder merged 1 commit intothebuilder:mainfrom
djk01281:fix/triggeronce-merged-refs
Feb 18, 2026
Merged

prevent callback skipping with triggerOnce and merged refs#747
thebuilder merged 1 commit intothebuilder:mainfrom
djk01281:fix/triggeronce-merged-refs

Conversation

Copy link
Contributor

djk01281 commented Feb 3, 2026 *
edited
Loading

Hello! I ran into this bug while using multiple useInView hooks with merged refs, so I opened an issue and put together this fix.

Let me know if anything needs to be changed!

Loving the library by the way : )

Description

Fixes #746

This PR fixes a bug where callbacks are skipped when multiple useInView({ triggerOnce: true }) hooks are merged on the same element.

Problem

When a callback executes with triggerOnce: true, it calls unobserve() which removes the callback from the array using splice(). This modifies the array during forEach iteration, causing subsequent callbacks to be skipped.

Solution

Copy the callbacks array before iterating using the spread operator:

[...elements.get(entry.target) ?? []].forEach((callback) => {
callback(inView, entry);
});

This prevents the iteration from being affected by array modifications.

Changes

  • src/observe.ts: Use spread operator to copy callbacks array before iteration
  • src/tests/useInView.test.tsx: Add test case to verify all callbacks are triggered with triggerOnce and merged refs

Testing

Added a new test that reproduces the bug:

  • Creates 3 useInView hooks with triggerOnce: true
  • Merges all refs onto a single element
  • Verifies all 3 hooks receive inView: true

Before fix: Test fails - only hooks 1 and 3 trigger, hook 2 remains false


After fix: Test passes - all 3 hooks trigger correctly : )

Copy link

bolt-new-by-stackblitz bot commented Feb 3, 2026

Run & review this pull request in StackBlitz Codeflow.

Copy link

vercel bot commented Feb 3, 2026 *
edited
Loading

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
react-intersection-observer Ready Preview, Comment Feb 3, 2026 1:36pm

vercel bot deployed to Preview February 3, 2026 13:28 View deployment
vercel bot deployed to Preview February 3, 2026 13:30 View deployment
djk01281 force-pushed the fix/triggeronce-merged-refs branch from 965a07c to 9b4e330 Compare February 3, 2026 13:32
vercel bot deployed to Preview February 3, 2026 13:33 View deployment
djk01281 force-pushed the fix/triggeronce-merged-refs branch from 9b4e330 to 50f709d Compare February 3, 2026 13:35
vercel bot deployed to Preview February 3, 2026 13:36 View deployment
Copy link

pkg-pr-new bot commented Feb 6, 2026

npm i https://pkg.pr.new/thebuilder/react-intersection-observer@747

commit: 50f709d

Copy link
Owner

thebuilder commented Feb 6, 2026

Looks good, thanks for fixing it. On vacation, but will release it in a couple of weeks.

djk01281 reacted with heart emoji

thebuilder requested a review from Copilot February 7, 2026 10:06
Copilot started reviewing on behalf of thebuilder February 7, 2026 10:07 View session
Copilot AI reviewed Feb 7, 2026
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes a regression/edge-case in the IntersectionObserver callback dispatch where multiple useInView({ triggerOnce: true }) listeners attached to the same element (via merged refs) could be skipped due to in-loop mutation of the callbacks array.

Changes:

  • Prevent callback skipping by iterating over a copied callbacks array in the observer entry handler.
  • Add a regression test covering multiple triggerOnce hooks merged onto a single element.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/observe.ts Iterates over a snapshot of the callbacks list to avoid splice() mutation during iteration.
src/__tests__/useInView.test.tsx Adds a test reproducing and validating the fix for merged refs + triggerOnce.

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

thebuilder merged commit cfb2fc6 into thebuilder:main Feb 18, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

Copilot code review Copilot Copilot left review comments

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Bug: Callbacks skipped when using triggerOnce with merged refs

3 participants