Skip to content

[Compiler Bug/Discussion]: react-hooks/preserve-manual-memoization Questions #34680

@cpojer

Description

@cpojer

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/#N4Igzg9grgTgxgUxALhASwLYAcIwC4AEAZmgDZ4IwByEAJgmMTBBgQOQACAdgNYBeAdwD0cXAiElylGvTAA6AFZg2AbgA6XTDnwEAyngCGcHgBoCwAgDV9RngQC+TFu279hYQ8dUatuQhYAlBCIAeQAjBQQ4PDMoMAQAUSIiKJiCOIQAWQQMCFj4oKIHJ1Y2GAQjPG9NbD9zAgBzGAMsAAsAR1J8hAAFAwa0LgM8NAguADFmhowELkJHImZS8sqAWnLSAwBPIVaICB55JWrfHQsAISg8PDHixec2OTkhKDQhS+uxuTwwAA8T2pnAgAKQgYQAMmgPAB9co4MBoG4wLYAEh4CC2dyW7Ge0OhDVmlGGCFoeKEoIhULwsIQ8MRuC2ciaLQ6pG+ynUNW0-hBYMhHgAilBKJiFtjHkI8QSuESKKToeS+VShSKmc02p12QDuQQgnSkVsKfy8ABJCise6lZ56iAIg1GqlmnLs-6cjSiLgeAiiKBzAgAXgIAEYAAxumW-HV4LZYBC64IIPBwVoOjzjLgB+N4WBcAAqMYQAB4NAQCNHYxAihk+gMhiMxpN+jM5hoAHwAbTKCaTrTYAF1wwhI3V6EQDFByMRfdFRhnU3gABTAEsEcopHuFEwruG2+nIrdcezIczb7vJwoAfmPhXCkWihcKieT8-TBAAPgQuBPSK3OaWd3aDLHvONL6gyaIYpy9gAJQnhm3pjF6Fi0MMBhmK0BhgFQQ5pFC4IQAYtCDA02G-GkpAEbQpFpGuT6tMU-orqW1b9IMwyzo20yzIQha8pSgrCvufHGqBu4GhBWytgQC5MaWjTqqyAAGslyUwTbccJVKiYByIELcNo6V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mzVkrl1ijEAJgQAehIA0FA6BASEG8vUHoWVawhCwPWT0WIHhhEqggUgqwsD3MGOsCo0RViiGwGQSgQgiIeGqBoJcK4hAiBYFgMg7ExiZG+nFTKpBWmHiUexMAJAGAED6PgWppBCx3KgA8p5jlWxEyOeAIxAVtpDFIGAFAY4WUIHsEAA

Repro steps

I'm not sure if question or bug.

I upgraded to the latest version of eslint-plugin-react-hooks, enabled every compiler lint rule, and got 150+ errors in a reasonably sized codebase related to react-hooks/preserve-manual-memoization. The attached playground link comes from a smaller project with idiomatic React/Relay code where I saw similar issues.

I roughly understand that the compiler can do a little bit better than me in this example, but I still prefer writing useCallback/useMemo in case I need to disable the compiler. I prefer manual control over memoization, and deferring "the rest" to the compiler. I realize that's subjective, so this is more of a question of whether this rule will be on by default in the recommended config in eslint-plugin-react-hooks? If yes, will it be a warning or an error?

Depending on the answer, I would like to ask if it's possible to change the name, title and explanation of this lint rule to something more approachable. Right now it says:

react-hooks/preserve-manual-memoization

Compilation Skipped: Existing memoization could not be preserved
React Compiler has skipped optimizing this component because the existing manual memoization could not be preserved. This value was memoized in source but not in compilation output.

The wording is imho quite confusing. The compiler is telling me I did something wrong, and is giving me no hints about what that might be. How about this:

react-hooks/unnecessary-manual-memoization

Compilation Skipped: Unnecessary manual memoization
React Compiler has skipped optimizing this component because the manual memoization is weaker than React Compiler's memoization. Remove the useMemo call and embed the result directly in your render function.

My final question is: if you can detect that React Compiler can do a better than me, couldn't you wrap my memoization with some compiler magic and get rid of this lint rule?

How often does this bug happen?

Every time

What version of React are you using?

19.2.0.

What version of React Compiler are you using?

I don't know, latest.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions