Skip to content

[compiler] Fix custom hook with use() not being compiled#661

Open
everettbu wants to merge 2 commits into
mainfrom
compiler-hook-with-use-api-fix
Open

[compiler] Fix custom hook with use() not being compiled#661
everettbu wants to merge 2 commits into
mainfrom
compiler-hook-with-use-api-fix

Conversation

@everettbu

Copy link
Copy Markdown

Mirror of facebook/react#35964
Original author: carlbergman


Summary

Fix detection of use() in custom hooks for infer mode. The isHookName check used the regex /^use[A-Z0-9]/ which doesn't match use. This caused custom hooks whose only invocation is use() to be skipped in infer mode.

This is the second PR per the development guide. The first PR is react/react#35963.

Fixes react/react#35960

How did you test this change?

yarn snap

Custom hooks whose only invocation is the `use()` function are
skipped by the compiler. This fixture demonstrates the
current (broken) output where `useMyContext` is not compiled.

Ref #35960
The `isHookName` check in Program.ts used the regex `/^use[A-Z0-9]/`
which doesn't match `use()`. This caused custom hooks whose only
invocation is `use()` to be skipped in infer mode.

Fixes #35960
@greptile-apps

greptile-apps Bot commented Mar 5, 2026

Copy link
Copy Markdown

Greptile Summary

Fixes a bug where custom hooks whose only hook invocation is React's use() were skipped during compilation in infer mode. The root cause was the isHookName regex (/^use[A-Z0-9]/) which requires a character after use, thereby not matching the bare name use. The fix adds || s === 'use' to the local isHookName in Program.ts.

  • Correctly handles the use() API in infer-mode hook detection by special-casing the exact string 'use'
  • Adds a test fixture (use-operator-in-hook) that validates a custom hook calling only use() is now compiled
  • Note: a separate exported isHookName in Environment.ts still uses the old regex — it works for standard React imports because use is a known global, but may want the same fix for consistency

Confidence Score: 4/5

  • This PR is safe to merge — it's a targeted, well-tested bug fix with minimal risk of side effects.
  • The fix is simple, correct, and well-scoped. It addresses a clear bug (the use() hook not being recognized) with a straightforward addition. The new test fixture validates the fix. The only minor concern is the duplicate isHookName in Environment.ts not being updated, though it's mitigated by use being a known global.
  • Consider also updating compiler/packages/babel-plugin-react-compiler/src/HIR/Environment.ts (line 1037-1039) which has a duplicate isHookName with the old regex.

Important Files Changed

Filename Overview
compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Program.ts Fixes isHookName to recognize use (React's built-in hook) by adding `
compiler/packages/babel-plugin-react-compiler/src/tests/fixtures/compiler/use-operator-in-hook.js New test fixture exercising infer-mode compilation of a custom hook (useMyContext) that only calls use(). Validates the bug fix.
compiler/packages/babel-plugin-react-compiler/src/tests/fixtures/compiler/use-operator-in-hook.expect.md Expected output for the new fixture, showing the compiler now correctly memoizes the useMyContext hook when using use() in infer mode.

Fix All in Claude Code Fix All in Codex

Last reviewed commit: 2d02a52


function isHookName(s: string): boolean {
return /^use[A-Z0-9]/.test(s);
return /^use[A-Z0-9]/.test(s) || s === 'use';

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duplicate isHookName not updated

There is a separate exported isHookName function in Environment.ts:1037 that still uses the old regex /^use[A-Z0-9]/ without the || s === 'use' addition. While use is already registered as a known global in Globals.ts (so the Environment.ts version may not cause issues for standard React imports), the two functions could drift out of sync and cause subtle bugs for edge cases (e.g., module-local bindings named use). Consider applying the same fix there for consistency, or refactoring to share a single isHookName implementation.

Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Program.ts
Line: 898

Comment:
**Duplicate `isHookName` not updated**

There is a separate exported `isHookName` function in `Environment.ts:1037` that still uses the old regex `/^use[A-Z0-9]/` without the `|| s === 'use'` addition. While `use` is already registered as a known global in `Globals.ts` (so the `Environment.ts` version may not cause issues for standard React imports), the two functions could drift out of sync and cause subtle bugs for edge cases (e.g., module-local bindings named `use`). Consider applying the same fix there for consistency, or refactoring to share a single `isHookName` implementation.

How can I resolve this? If you propose a fix, please make it concise.

Fix in Claude Code Fix in Codex

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.

[Compiler Bug]: React Compiler silently skips custom hooks whose only hook call is use()

2 participants