Skip to content

Commit e1997ce

Browse files
committed
Fix className hydration warning for custom elements
1 parent 3499590 commit e1997ce

File tree

3 files changed

+16
-9
lines changed

3 files changed

+16
-9
lines changed

packages/react-dom-bindings/src/client/DOMPropertyOperations.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -253,10 +253,6 @@ export function getValueForAttributeOnCustomComponent(
253253
}
254254
return expected === undefined ? undefined : null;
255255
}
256-
if (enableCustomElementPropertySupport && name === 'className') {
257-
// className is a special cased property on the server to render as an attribute.
258-
name = 'class';
259-
}
260256
const value = node.getAttribute(name);
261257

262258
if (enableCustomElementPropertySupport) {

packages/react-dom-bindings/src/client/ReactDOMComponent.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,6 +1063,20 @@ function diffHydratedCustomComponent(
10631063
}
10641064
continue;
10651065
}
1066+
case 'className':
1067+
if (enableCustomElementPropertySupport) {
1068+
// className is a special cased property on the server to render as an attribute.
1069+
extraAttributeNames.delete('class');
1070+
const serverValue = getValueForAttributeOnCustomComponent(
1071+
domElement,
1072+
'class',
1073+
nextProp,
1074+
);
1075+
if (nextProp !== serverValue) {
1076+
warnForPropDifference('className', serverValue, nextProp);
1077+
}
1078+
continue;
1079+
}
10661080
// eslint-disable-next-line no-fallthrough
10671081
default: {
10681082
let ownNamespaceDev = parentNamespaceDev;

packages/react-dom/src/__tests__/ReactDOMServerIntegrationAttributes-test.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -662,16 +662,13 @@ describe('ReactDOMServerIntegration', () => {
662662
});
663663

664664
itRenders('className for custom elements', async render => {
665+
const e = await render(<custom-element className="test" />, 0);
665666
if (ReactFeatureFlags.enableCustomElementPropertySupport) {
666-
const e = await render(
667-
<custom-element className="test" />,
668-
render === clientRenderOnServerString ? 1 : 0,
669-
);
670667
expect(e.getAttribute('className')).toBe(null);
671668
expect(e.getAttribute('class')).toBe('test');
672669
} else {
673-
const e = await render(<custom-element className="test" />, 0);
674670
expect(e.getAttribute('className')).toBe('test');
671+
expect(e.getAttribute('class')).toBe(null);
675672
}
676673
});
677674

0 commit comments

Comments
 (0)