Skip to content

Commit d4e2063

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

File tree

3 files changed

+18
-10
lines changed

3 files changed

+18
-10
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: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,6 +1064,21 @@ function diffHydratedCustomComponent(
10641064
continue;
10651065
}
10661066
// eslint-disable-next-line no-fallthrough
1067+
case 'className':
1068+
if (enableCustomElementPropertySupport) {
1069+
// className is a special cased property on the server to render as an attribute.
1070+
extraAttributeNames.delete('class');
1071+
const serverValue = getValueForAttributeOnCustomComponent(
1072+
domElement,
1073+
'class',
1074+
nextProp,
1075+
);
1076+
if (nextProp !== serverValue) {
1077+
warnForPropDifference('className', serverValue, nextProp);
1078+
}
1079+
continue;
1080+
}
1081+
// eslint-disable-next-line no-fallthrough
10671082
default: {
10681083
let ownNamespaceDev = parentNamespaceDev;
10691084
if (ownNamespaceDev === HTML_NAMESPACE) {

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function initModules() {
3434
};
3535
}
3636

37-
const {resetModules, itRenders, clientCleanRender, clientRenderOnServerString} =
37+
const {resetModules, itRenders, clientCleanRender} =
3838
ReactDOMServerIntegrationUtils(initModules);
3939

4040
describe('ReactDOMServerIntegration', () => {
@@ -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)