Fix: Prevent Stringification of Symbol and Function values in value/defaultValue attributes. #33951
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 addresses an incorrect behavior in React's DOM attribute handling: previously, values of type
Symbol
orFunction
passed to thevalue
ordefaultValue
props on elements were being coerced into strings and rendered into the DOM. This is not valid behavior, as these types are non-serializable and should be ignored.This PR ensures:
value
anddefaultValue
props withSymbol
orFunction
values are now ignored and excluded from the DOM.Symbol
orFunction
.Motivation
React typically ignores non-serializable or unsafe types like
Symbol
orFunction
for DOM attributes. However, prior to this change, these types were being stringified when used invalue
anddefaultValue
on input elements, resulting in unwanted output like "Symbol()" or "function(){}". This:This fix improves React's robustness and consistency by explicitly removing such attributes when
Symbol
orFunction
values are detected.How did you test this change?
To ensure this fix behaves correctly and does not introduce regressions, the following validation steps were performed:
This verified that passing a Symbol or Function to value or defaultValue correctly results in the attribute being omitted from the DOM.
Some snapshot mismatches were expected due to the corrected behavior. After verifying their correctness, snapshots were updated using:
This comprehensive testing ensures that the fix is safe, focused, and maintains React’s overall reliability.