Normalize className as token sets for hydration comparison #34940
      
        
          +50
        
        
          −0
        
        
          
        
      
    
  
  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.
  
    
  
    
Fixes #34939
This PR normalizes
classNamevalues as token sets during DEV hydration comparison, eliminating false-positive warnings when server and client produce semantically identical classes that differ only in whitespace, token order, or duplicates.Changes:
normalizeClassForHydration(markup: mixed): stringto normalize class stringswarnForPropDifferenceto compare normalized forms forclass/classNamepropsRationale:
As per the HTML spec,
classis an unordered set of space-separated tokens. React's currentclass/classNamestring comparison produces false positives that don't represent actual DOM differences.Specs:
Precedent:
warnForPropDifferenceto handle HTML parser behavior. This PR extends that pattern to handleclassattribute's token-set semantics.Summary
Motivation:
React currently throws hydration warnings when server and client
classNamevalues are semantically identical but differ only in representation (i.e. differing only by whitespace, token order, or duplicates). This is a false positive: the DOM result is identical as per the HTML spec, which definesclassas an unordered set of tokens.This issue has become increasingly common since the initial hydration normalization code (44c32fc) was written 8 years ago (2017):
This PR eliminates these false positives while preserving React's ability to catch genuine hydration mismatches.
Implementation
Added
HTML_SPACE_CLASS_SEPARATORconstant:Added normalization function:
Early return in
warnForPropDifferencefor normalized matches:How did you test this change?
Added tests:
Test runs:
yarn test react-dom- all tests passyarn test react-dom --prod- all tests passVerified that genuine mismatches still warn:
"flex"vs"grid") - still warns