Merged
Conversation
|
|
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
commit: |
There was a problem hiding this comment.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.