Skip to content

Fix: Prevent Stringification of Symbol and Function values in value/defaultValue attributes. #33951

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

saylik365
Copy link

Summary

This PR addresses an incorrect behavior in React's DOM attribute handling: previously, values of type Symbol or Function passed to the value or defaultValue 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:

  1. value and defaultValue props with Symbol or Function values are now ignored and excluded from the DOM.
  2. Such attributes are removed from the DOM if dynamically updated with a Symbol or Function.
  3. Clear console warnings appear in development mode, informing developers of incorrect usage.
  4. This fix brings the behavior in line with React’s handling of invalid or non-serializable attributes elsewhere in the system.

Motivation

React typically ignores non-serializable or unsafe types like Symbol or Function for DOM attributes. However, prior to this change, these types were being stringified when used in value and defaultValue on input elements, resulting in unwanted output like "Symbol()" or "function(){}". This:

  • Violates expected rendering behavior.
  • Produces confusing or incorrect DOM states.
  • Contradicts React's design philosophy of preventing unsafe or invalid attribute usage.

This fix improves React's robustness and consistency by explicitly removing such attributes when Symbol or Function 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:

  1. Ran the specific test suite to confirm correctness of the fix:

yarn test ReactDOMAttribute-test

This verified that passing a Symbol or Function to value or defaultValue correctly results in the attribute being omitted from the DOM.

  1. Reviewed and updated snapshots:

Some snapshot mismatches were expected due to the corrected behavior. After verifying their correctness, snapshots were updated using:

yarn test -u

  1. Executed the full test suite to confirm no regressions:

yarn test

  1. Ensured consistent code style:

yarn prettier
yarn lint

  1. Performed static type checking using Flow:

yarn flow

This comprehensive testing ensures that the fix is safe, focused, and maintains React’s overall reliability.

Copy link

meta-cla bot commented Jul 20, 2025

Hi @saylik365!

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 [email protected]. Thanks!

@saylik365
Copy link
Author

Hi maintainers 👋

This PR resolves an issue where elements were incorrectly rendering Symbol and Function values passed to value or defaultValue props by coercing them into strings. These types are now properly ignored, bringing behavior in line with React’s DOM attribute handling conventions. I've included the following to ensure correctness:

  • Added targeted unit tests and updated snapshots.
  • Verified full test suite passes with no regressions.
  • Ensured consistent formatting (prettier, lint).
  • Performed static type checks using Flow.

Please let me know if any revisions or additional context are needed. I’d be happy to assist further. Thank you for your time and review!

Copy link

meta-cla bot commented Jul 20, 2025

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

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

Successfully merging this pull request may close these issues.

1 participant