Skip to content

Fix callback ref cleanup compatibility across React 17–19#760

Merged
thebuilder merged 6 commits into
mainfrom
feat/002-react-ref-cleanup-compatibility
Jul 14, 2026
Merged

Fix callback ref cleanup compatibility across React 17–19#760
thebuilder merged 6 commits into
mainfrom
feat/002-react-ref-cleanup-compatibility

Conversation

@thebuilder

@thebuilder thebuilder commented Jul 14, 2026

Copy link
Copy Markdown
Owner

Summary

  • return callback-ref cleanup functions only for React 19 while preserving the React 17/18 ref(null) cleanup path
  • look up optional React APIs without creating required named-export assumptions in consumer bundles
  • add a build-only packed Rsbuild compatibility fixture for React 17, 18, and 19, while keeping runtime lifecycle coverage in the existing Vitest Browser matrix

Why

React 19 supports cleanup functions returned from callback refs, but React 17 and 18 treat that return value as invalid and emit diagnostics. useOnInView returned its cleanup unconditionally even though all three React majors are supported peers.

The private version gate is deliberately limited to 19. because that is the newest declared and tested peer major. This keeps React 19 merged-ref cleanup composition while falling back conservatively for older, future, and malformed version strings.

The packed fixture only verifies the consumer packaging and bundler boundary. Runtime cleanup and diagnostics are exercised in real Chromium by the existing Vitest Browser suite, avoiding a separate JSDOM runtime harness.

Fixes #755.

Verification

  • corepack pnpm build
  • corepack pnpm exec vitest run — 77 tests passed
  • corepack pnpm run compat:packed -- 17
  • corepack pnpm run compat:packed -- 18
  • corepack pnpm run compat:packed -- 19
  • corepack pnpm exec tsc --noEmit
  • corepack pnpm exec biome check src/useOnInView.tsx src/refCleanupSupport.ts src/__tests__/useOnInView.test.tsx scripts/compat fixtures/react-compat package.json
  • corepack pnpm dlx github-actionlint .github/workflows/test.yml
  • useOnInView: 1.06 kB / 1.1 kB size limit

@bolt-new-by-stackblitz

Copy link
Copy Markdown

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

@vercel

vercel Bot commented Jul 14, 2026

Copy link
Copy Markdown

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

Project Deployment Actions Updated (UTC)
react-intersection-observer Ready Ready Preview, Comment Jul 14, 2026 10:35am

@pkg-pr-new

pkg-pr-new Bot commented Jul 14, 2026

Copy link
Copy Markdown
npm i https://pkg.pr.new/react-intersection-observer@760

commit: 0d63313

@thebuilder
thebuilder marked this pull request as ready for review July 14, 2026 10:41
@thebuilder
thebuilder merged commit b97fbaa into main Jul 14, 2026
9 checks passed
@thebuilder
thebuilder deleted the feat/002-react-ref-cleanup-compatibility branch July 14, 2026 10:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make callback-ref cleanup compatible with React 17–19

1 participant