react / react/react

[Compiler Bug]: Compiler optimizes away useMemo with manual dependencies

Open
#33,559 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Compiler: todo Component: React Compiler Type: Bug
Dominant language
JavaScript
Stars
251k
Forks
51.4k
Avg merge
2d 4h
Merged PRs (30d)
53

Description

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-compiler (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/#N4Igzg9grgTgxgUxALhASwLYAcIwC4AEwBUYCAsghhAQL4EBmMEGBA5DAgIZx5sDcAHQB2IgPRiCAQQAmMgmQA2DAGLCAFADcuiqAgCUBNMLBoZCAngAWF0hSo08NMhesWEADywJeCeQCMEKy5NNFwROAgTQiVVYQIAXgIPRIA+ZKFRYU8cfEYoYV4w+LsASXNhPDQ8AE8tHT0AGiMKqtq1Q2ARAgJI6JaESuqaxIGh9o1tXQNMns48WBKySmp1dUME9Prp-WaAbTNBtpqAXX1M2hEQWiA

Repro steps

We have a hook like this that basically returns the previous value if the calculated identity of the value hasn't changed (e.g. Dayjs object and its unix timestamp)

function useIdentity(value, identityFn) {
  const identity = identityFn(value);
  return useMemo(() => value, [identity]);
}

I noticed the tests for this were failing after enabling React Compiler.

This hook violates the rules-of-react and has the linter directives to disable it. But for some reason the compiler isn't able to detect the violation.

If I write the hook like this, the optimization is skipped as expected.

const selfFn = x => x;
function useIdentity(value, identityFn) {
  const identity = identityFn(value);
  return useMemo(() => selfFn(value), [identity]);
}

So I guess the hook doesn't respect the manual optimization because the () => value expression is too simple and thinks it can be optimized without issues.

How often does this bug happen?

Every time

What version of React are you using?

19.1.0

What version of React Compiler are you using?

19.1.0-rc.2

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the linked React Playground reproduction and compare the compiled behavior for the two useIdentity variants. Investigate the React Compiler core handling of useMemo with manual dependencies; done means the compiler no longer optimizes away the first variant and the reported failing tests pass.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, react
Domain
compilers, frontend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.