Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Fix className hydration warning for custom elements
  • Loading branch information
sebmarkbage committed Mar 30, 2023
commit d4e2063509a3460e63f7d89c2c7c7a1f575129ec
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,6 @@ export function getValueForAttributeOnCustomComponent(
}
return expected === undefined ? undefined : null;
}
if (enableCustomElementPropertySupport && name === 'className') {
// className is a special cased property on the server to render as an attribute.
name = 'class';
}
const value = node.getAttribute(name);

if (enableCustomElementPropertySupport) {
Expand Down
15 changes: 15 additions & 0 deletions packages/react-dom-bindings/src/client/ReactDOMComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -1064,6 +1064,21 @@ function diffHydratedCustomComponent(
continue;
}
// eslint-disable-next-line no-fallthrough
case 'className':
if (enableCustomElementPropertySupport) {
// className is a special cased property on the server to render as an attribute.
extraAttributeNames.delete('class');
const serverValue = getValueForAttributeOnCustomComponent(
domElement,
'class',
nextProp,
);
if (nextProp !== serverValue) {
warnForPropDifference('className', serverValue, nextProp);
}
continue;
}
// eslint-disable-next-line no-fallthrough
default: {
let ownNamespaceDev = parentNamespaceDev;
if (ownNamespaceDev === HTML_NAMESPACE) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function initModules() {
};
}

const {resetModules, itRenders, clientCleanRender, clientRenderOnServerString} =
const {resetModules, itRenders, clientCleanRender} =
ReactDOMServerIntegrationUtils(initModules);

describe('ReactDOMServerIntegration', () => {
Expand Down Expand Up @@ -662,16 +662,13 @@ describe('ReactDOMServerIntegration', () => {
});

itRenders('className for custom elements', async render => {
const e = await render(<custom-element className="test" />, 0);
if (ReactFeatureFlags.enableCustomElementPropertySupport) {
const e = await render(
<custom-element className="test" />,
render === clientRenderOnServerString ? 1 : 0,
);
expect(e.getAttribute('className')).toBe(null);
expect(e.getAttribute('class')).toBe('test');
} else {
const e = await render(<custom-element className="test" />, 0);
expect(e.getAttribute('className')).toBe('test');
expect(e.getAttribute('class')).toBe(null);
}
});

Expand Down