Skip to content

Commit

Permalink
Fix className hydration warning for custom elements
Browse files Browse the repository at this point in the history
  • Loading branch information
sebmarkbage committed Mar 30, 2023
1 parent 3499590 commit e1314a2
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,10 @@ export function getValueForAttributeOnCustomComponent(
if (!isAttributeNameSafe(name)) {
return;
}
if (enableCustomElementPropertySupport && name === 'className') {
// className is a special cased property on the server to render as an attribute.
name = 'class';
}
if (!node.hasAttribute(name)) {
// shouldRemoveAttribute
switch (typeof expected) {
Expand All @@ -253,10 +257,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
Original file line number Diff line number Diff line change
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

0 comments on commit e1314a2

Please sign in to comment.