# Fix: Allow mutating ref.current when ref comes from useContext() #32968
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR fixes a React Compiler bug where mutating the
.currentproperty of a ref that was obtained throughuseContext()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
.currentproperty as invalid, even though mutating a ref's.currentproperty is a valid and common React pattern.The fix adds a special case in
inferOperandEffectto detect when a property namedcurrentis being mutated and the value has theContextreason, allowing proper mutation of ref objects passed through context.How did you test this change?
Added a test case (
refContext.jsand corresponding.expect.md) that demonstrates the pattern working correctly with the fix.The test case includes:
.currentpropertyVerified that with the fix, the compiler no longer throws the error when mutating the ref's current property.
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
.currentproperty mutated without triggering the error.Ran the compiler test suite to ensure no regressions were introduced.
Fixes: #31470