-
Notifications
You must be signed in to change notification settings - Fork 2
fix: use useCallback as trackingRef to observe ref changes #1442
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,7 +9,10 @@ export interface UseIntersectionOptions { | |
| } | ||
|
|
||
| const VISIBILITY_POLL_INTERVAL = 250; | ||
| export const useIntersectionAdvanced = (ref: MutableRef<HTMLElement | null>, options: UseIntersectionOptions = {}): boolean => { | ||
| export const useIntersectionAdvanced = ( | ||
| ref: MutableRef<HTMLElement | null>, | ||
| options: UseIntersectionOptions = {} | ||
| ): { inViewport: boolean; updateRef: (el: HTMLElement | null) => void } => { | ||
| const { rootMargin = '0px', fireOnce = false, threshold = 0, minVisibleTime = 0, resetKey } = options; | ||
| // State and setter for storing whether element is visible | ||
| const [isIntersecting, setIntersecting] = useState<boolean>(false); | ||
|
|
@@ -22,6 +25,15 @@ export const useIntersectionAdvanced = (ref: MutableRef<HTMLElement | null>, opt | |
| // Track the last reset key to detect changes | ||
| const lastResetKeyRef = useRef<string | undefined>(resetKey); | ||
|
|
||
| const [counter, setCounter] = useState(0); | ||
| const updateRef = (el: HTMLElement | null) => { | ||
| // setting ref.current does not update useEffect, counter used to force useEffect | ||
| if (!ref.current || ref.current !== el) { | ||
| ref.current = el; | ||
| setCounter((c) => c + 1); | ||
| } | ||
| }; | ||
|
|
||
| // Reset state if resetKey has changed | ||
| if (resetKey !== lastResetKeyRef.current) { | ||
| setIntersecting(false); | ||
|
||
|
|
@@ -31,7 +43,7 @@ export const useIntersectionAdvanced = (ref: MutableRef<HTMLElement | null>, opt | |
| } | ||
| visibleStartRef.current = null; | ||
| lastResetKeyRef.current = resetKey; | ||
| return false; | ||
| return { inViewport: false, updateRef }; | ||
| } | ||
|
|
||
| useEffect(() => { | ||
|
|
@@ -140,9 +152,9 @@ export const useIntersectionAdvanced = (ref: MutableRef<HTMLElement | null>, opt | |
| observer.unobserve(ref.current); | ||
| } | ||
| }; | ||
| }, [ref, resetKey]); | ||
| }, [ref, resetKey, counter]); | ||
|
|
||
| return isIntersecting; | ||
| return { inViewport: isIntersecting, updateRef }; | ||
| }; | ||
|
|
||
| function elementIsVisible(el: HTMLElement): boolean { | ||
|
|
||
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.
updateRefis recreated on every render, which makes downstream memoization (e.g., theuseCallbackinwithTracking) ineffective and can cause extra rerenders. WrapupdateRefinuseCallback(and, if needed, switchcountertouseReducer/useRef-based invalidation) so consumers can keep a stable function reference.