Skip to content

Conversation

@Vansh16aug
Copy link

@Vansh16aug Vansh16aug commented Apr 20, 2025

Summary

This PR fixes a React Compiler bug where mutating the .current property of a ref that was obtained through useContext() would incorrectly trigger an error: "Mutating a value returned from 'useContext()', which should not be mutated".

When a ref is passed through context and retrieved in a child component, the compiler was incorrectly flagging mutations to its .current property as invalid, even though mutating a ref's .current property is a valid and common React pattern.

The fix adds a special case in inferOperandEffect to detect when a property named current is being mutated and the value has the Context reason, allowing proper mutation of ref objects passed through context.

How did you test this change?

  1. Added a test case (refContext.js and corresponding .expect.md) that demonstrates the pattern working correctly with the fix.

  2. The test case includes:

    • A parent component that creates a ref and passes it through context
    • A child component that retrieves the ref from context and mutates its .current property
    • Both useEffect and event handler scenarios for the mutation
  3. Verified that with the fix, the compiler no longer throws the error when mutating the ref's current property.

  4. Manually tested with code that reproduces the issue reported in [Compiler Bug]: "Mutating a value returned from 'useContext()', which should not be mutated" When the value is a Ref #31470, confirming that refs passed through context can now have their .current property mutated without triggering the error.

  5. Ran the compiler test suite to ensure no regressions were introduced.

Fixes: #31470

@facebook-github-bot
Copy link
Contributor

Hi @Vansh16aug!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Compiler Bug]: "Mutating a value returned from 'useContext()', which should not be mutated" When the value is a Ref

2 participants