You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Bug: react-hooks/refs false positive using IntersectionObserver #35982
Attempting to create a simple hook to wrap IntersectionObserver:
functionuseIntersectionObserver(options: Partial<IntersectionObserverInit>){constcallbacks=useRef(newMap<string,IntersectionCallback>());constonIntersect=useCallback((entries: ReadonlyArray<IntersectionObserverEntry>)=>{entries.forEach(entry=>callbacks.current.get(entry.target.id)?.(entry.isIntersecting))},[]);constobserver=useMemo(()=>// This line incorrectly reports "Error: Cannot access refs during render"newIntersectionObserver(onIntersect,options),[onIntersect,options]);// Some other stuff here}
Link to code example:
The current behavior
The above code reports the following when running eslint:
error Error: Cannot access refs during render
React refs are values that are not needed for rendering. Refs should only be accessed outside of render,
such as in event handlers or effects. Accessing a ref value (the `current` property) during render can
cause your component not to update as expected (https://react.dev/reference/react/useRef).
common\ReactHooks.ts:97:30
|
| const observer = useMemo(() =>
| new IntersectionObserver(onIntersect, options), [onIntersect, options]);
| ^^^^^^^^^^^ Cannot access ref value during render
|
The expected behavior
The code is not using a ref (at least, not directly) and anyway it isn't rendering - so it's a mystery as to why the error is reported, unless I'm missing some subtle behaviour?
This seems to be a systematic issue, rather than being specifically about IntersectionObserver. I'm running into the same error with React Native's PanResponder:
This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize it yet. If you have any new additional information, please include it with your comment!
I'd like to take this on. I noticed the two earlier PRs (#36125, #36418) were closed by the stale bot without review, so I'll investigate the false positive and open a fresh PR.
Activity
Fix: Resolve ESLint false positive and improve error messages
khalidsaidi commentedon Mar 25, 2026
perf(web): enable React Compiler
perf(web): enable React Compiler (#103)
SimpleCreations commentedon Jun 16, 2026
This seems to be a systematic issue, rather than being specifically about
IntersectionObserver. I'm running into the same error with React Native'sPanResponder:How exactly does React Compiler determine which functions are run "during render"? There is nothing in this code that suggests that.
CC @josephsavona
github-actions commentedon Sep 22, 2026
This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize it yet. If you have any new additional information, please include it with your comment!
SimpleCreations commentedon Sep 22, 2026
Not stale
akasakariko commentedon Sep 28, 2026
I'd like to take this on. I noticed the two earlier PRs (#36125, #36418) were closed by the stale bot without review, so I'll investigate the false positive and open a fresh PR.
[compiler] Fix refs false positive for IntersectionObserver callbacks (…
SimpleCreations commentedon Oct 22, 2026
Since the root cause wasn't addressed in the linked PR, I've opened another issue reproducing the same bug with React Native's
PanResponder: #37955