Skip to content

[Compiler]: flags mutations of shared mutable refs and skips optimizations #33209

Closed

Description

@szhsin

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/#N4Igzg9grgTgxgUxALhASwLYAcIwC4AEwBcMCAhnggMIQB2VAHngDQFRgIBKCAZmxxr0mhAL4FeMCBgIAdEGXJw88gNyy6GuPTCFaUBghg9etQ8wIBeEoqpmRACgCU6zXW11dBAGIQIVgmcrAD4iDQISHUJtAypjPgDBewRmB31DeNNhFLwXDXCCMjxYOkCCiIAeACMoPDx6coiCemoAGzQ4AGtLYCDLUOBGpoI0XkCYjJMnSNijEwA6OFgyBgIAamsARldhiNFRRuChggBBABMzxoqAehq6+iPSgjy6UVcNFJx8SM9CE6wsAE+gMCtdrgQAMoAC3IZDObAwtXIVVaCAIdHoAFoAKoASQIADdyK0oAhQeCIfU4SNSuRCgl6gRyASIGgzgQ8DA0ABzblGemYlZnIxoOjcggAdyhCFKeGlhOJpJIMLFCDABQ8XgmcRMiU4JgcAAYXgUiiUyk9KukdXxksx5gAFKQEtn8okkhA9bVzPiiR67AgVXz+a7+3ZBvwEUPHCMhsOVa7Wn1Zcx4R3O10wMMvN4aECiIA

Repro steps

Sometimes you need a shared, mutable value that isn’t tied to the UI but is used across multiple components. In these cases, you don’t want React to trigger re-renders when the value changes. To handle this, you can store the value in a ref and pass it down through context.

However, when a child component mutates this value (for example, inside an event handler), the compiler currently produces an error and causes optimizations to be skipped. I feel this is too restricted for what is otherwise a legitimate React pattern.

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.1

Activity

  1. added
    Status: Unconfirmed
    on May 15, 2025
  2. removed
    Status: Unconfirmed
    on Jun 23, 2025
  3. josephsavona commented on Jun 23, 2025

    @josephsavona
    Contributor

    Thanks for posting. This is occurring because we are currently very conservative about what we assume is a ref. I think the correct thing is to treat identifiers which look like refs as if they were refs (ref or -Ref suffix). We have a compiler flag for this that we should turn on.

  4. josephsavona commented on Jul 29, 2025

    @josephsavona
    Contributor

    #34005 enables the flag to treat ref-like identifiers as refs by default. As long as your variable is named ref or ends in Ref (myRef etc), the compiler will treat it as a ref. Thanks again for posting!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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