-
Notifications
You must be signed in to change notification settings - Fork 189
feat: implement the useOnInView hook #738
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
|
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
commit: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR implements the useOnInView hook, a new alternative to useInView that provides a callback-based API for monitoring element visibility without triggering re-renders. This is useful for performance-critical use cases like tracking and logging.
Key changes:
- Added
useOnInViewhook with callback-based API and optional cleanup function - Introduced
triggeroption to specify whether callbacks fire on enter or leave - Comprehensive test coverage for various scenarios including threshold tracking, cleanup behavior, and observer destruction
Reviewed Changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
src/useOnInView.tsx |
Core implementation of the new hook with ref callback pattern and observer lifecycle management |
src/index.tsx |
Exported new hook and added related TypeScript types for the API |
src/__tests__/useOnInView.test.tsx |
Complete test suite covering callback execution, cleanup, thresholds, and edge cases |
storybook/stories/useOnInView.story.tsx |
Interactive Storybook examples demonstrating the hook's features |
README.md |
Documentation for the new hook with usage examples and options table |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Based on the great work in #718 by @jantimon - This is an attempt to implement just the
useOnInViewhook, while maintaining the fallback functionality, and legacy React version support.✨ New
useOnInViewhook — a no-re-render alternative touseInViewthat delivers(inView, entry)to your callback while returning a ref you can attach to any element. Designed for tracking, analytics, and other side effect heavy workloads where state updates are unnecessary.IntersectionChangeEffect/IntersectionEffectOptionstypes — exported helper types that describe the new hook’s callback and options surface.useOnInView.✨ Improvements
useInView,useOnInView, and<InView>now ignore the browser’s initialinView === falseemission so handlers only fire once a real visibility change occurs, while still reporting all subsequent enter/leave transitions (including threshold arrays).skiptoggles and fallback scenarios re-attach correctly without losing previous state.🧪 Testing
useOnInView, covering thresholds,triggerOnce,skiptoggling, merged refs, and multiple observers on the same node.