Skip to content

[Compiler Bug]: False positive on "Hooks may not be referenced as normal values, they must be called" #35326

@oliverlaz

Description

@oliverlaz

What kind of issue is this?

  • React Compiler core (the JS output is incorrect, or your app works incorrectly after optimization)
  • babel-plugin-react-compiler (build issue installing or using the Babel plugin)
  • eslint-plugin-react-hooks (build issue installing or using the eslint plugin)
  • react-compiler-healthcheck (build issue installing or using the healthcheck script)

Link to repro

https://playground.react.dev/#N4Igzg9grgTgxgUxALhHCA7MAXABFMBANQEMAbKBXAXlwAoBKGgPn0IFkEBbCOxl3ABYATABpcAbQC6DANwAdDOix4AFhAgBrMDVzA2xcpVwBfBRkXKcuAGIkAlmR21+1VsEW5cVvPoKGKKhNddS0wcy8fXAA3Iypaf1JAxgjcGARsWAxcAB4wAAcSDGZgWMCTHIB6AqLmRRNFS0xrAHUIGG1dV3dPb2bfAyTKZBjTEI1tVKiy41polN70zJhsvMLi0riK6vW6jAaMEFE0TAAzewBzFBB7Lnz2vGwAT3yqfQAFCgv7DAB5fOw9maY1OMAgXFwAHIAEYkaEIMgAWnyXx+iPSJDg2ER6DujgQMEqABN7DhIeZFHQPPtcGASICwOcEDpPlBvn8AUCsHIjuB1AB3ACSGGwBIw5DAKFOEoQJiAA

Repro steps

The React Compiler doesn't correctly flag improper hook usage, when using a "hooks provider".
Although the hook is always called, React Compiler fails with the following message:

 Hooks may not be referenced as normal values, they must be called.

It seems the fact that the hook variable name starts with use_ is causing the compiler assume its usage.

const useValue = () => useMemo(() => 42, []);
const hooks = { useValue };

const Fails = () => {
  const { useValue } = hooks;
  const value = useValue();
  //            ^^^^^^^^ Hooks may not be referenced as normal values, they must be called.
  return <span>{value}</span>
}

const Works = () => {
  const { useValue: v } = hooks;
  const value = v(); // works as expected
  return <span>{value}</span>
}

How often does this bug happen?

Every time

What version of React are you using?

19.2.1

What version of React Compiler are you using?

1.0.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Status: UnconfirmedA potential issue that we haven't yet confirmed as a bugType: Bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions