[compiler] Allow strict comparisons of ref-accessing callbacks by kolvian · Pull Request #37528 · react/react · GitHub
Skip to content

[compiler] Allow strict comparisons of ref-accessing callbacks - #37528

Open
kolvian wants to merge 1 commit into
react:mainfrom
kolvian:fix-37521-ref-comparisons
Open

[compiler] Allow strict comparisons of ref-accessing callbacks#37528
kolvian wants to merge 1 commit into
react:mainfrom
kolvian:fix-37521-ref-comparisons

Conversation

@kolvian

@kolvian kolvian commented Sep 6, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #37521. Previously, strict equality comparisons (ie. === or !==) with ref-accessing callback functions as operands would be flagged by ESLint as accessing ref values at render time. Since strict equality comparisons don't call the operand functions, it is safe to perform these comparisons at render time, so I add narrow support to === and !==, while still rejecting operands read from ref.current. In the future, this could be expanded to include typeof and other checks as well. I also added 8 tests for thoroughness:

4 compiler fixtures, each with a generated .expect.md snapshot:

  • Safe strict comparisons of callbacks, including aliases and conditional selection.
  • Comparisons involving actual ref values still warn.
  • Calling ref-accessing callbacks inside comparisons still warns.
  • Loose comparisons retain existing warnings.

4 ESLint 8 integration cases:

  • The issue’s TypeScript reproduction produces no warning.
  • Comparing a ref value warns.
  • Comparing a function read from a ref warns.
  • Calling a ref-accessing callback in a comparison warns.

How did you test this change?

All tests pass, yarn passes, yarn flow dom-node passes. I also added a specific regression test for this change, which runs something similar to this:

import {useRef} from 'react';

function Component({previous}) {
  const ref = useRef(0);
  const callback = () => ++ref.current;

  return callback !== previous;
}

Previously, this would be flagged by ESLint. You can run this yourself by running:

cd compiler
yarn snap -p '*ref*comparison*'

# ESLint integration tests
cd ../packages/eslint-plugin-react-hooks
yarn test ReactCompilerRuleTypescript --runInBand

@meta-cla meta-cla Bot added the CLA Signed label Sep 6, 2026
@github-actions

github-actions Bot commented Sep 6, 2026

Copy link
Copy Markdown

@kolvian
kolvian force-pushed the fix-37521-ref-comparisons branch from a4233ba to 726e34e Compare September 6, 2026 08:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: ESLint flags just refering (not calling) functions that use refs as accessing refs (react-hooks/refs)

1 participant