Skip to content

Bug: react-hooks/refs false positive using IntersectionObserver #35982

Closed

Description

@ashleymercer

React version: 17.0.2
eslint-plugin-react: 7.37.5
eslint-plugin-react-hooks: 7.0.1

Steps To Reproduce

Attempting to create a simple hook to wrap IntersectionObserver:

function useIntersectionObserver(options: Partial<IntersectionObserverInit>) {

  const callbacks = useRef(new Map<string, IntersectionCallback>());

  const onIntersect = useCallback((entries: ReadonlyArray<IntersectionObserverEntry>) => {
    entries.forEach(entry => callbacks.current.get(entry.target.id)?.(entry.isIntersecting)) },
    []
  );

  const observer = useMemo(() => 
    // This line incorrectly reports "Error: Cannot access refs during render"
    new IntersectionObserver(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?

Activity

  1. added
    Status: Unconfirmed
    on Mar 9, 2026
  2. added a commit that references this issue on Mar 21, 2026
    4f64173
  3. khalidsaidi commented on Mar 25, 2026

    @khalidsaidi
  4. SimpleCreations commented on Jun 16, 2026

    @SimpleCreations

    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:

      const swipeAxisRef = useRef<SwipeGestureAxis>(undefined);
    
     // ...
    
      const panResponder = PanResponder.create({
        onMoveShouldSetPanResponder: (_, gesture) => {
          const axis = getGestureAxis(gesture.dx, gesture.dy);
          if (axis == null) {
            return false;
          }
    
          swipeAxisRef.current = axis;
          return true;
        },

    How exactly does React Compiler determine which functions are run "during render"? There is nothing in this code that suggests that.

    CC @josephsavona

  5. github-actions commented on Sep 22, 2026

    @github-actions

    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!

  6. added
    Resolution: Stale
    on Sep 22, 2026
  7. SimpleCreations commented on Sep 22, 2026

    @SimpleCreations

    Not stale

  8. removed
    Resolution: Stale
    on Sep 22, 2026
  9. akasakariko commented on Sep 28, 2026

    @akasakariko
    Contributor

    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.

  10. added a commit that references this issue on Oct 14, 2026
    efa3189
  11. SimpleCreations commented on Oct 22, 2026

    @SimpleCreations

    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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Status: Unconfirmed

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions