Skip to content

Commit 1c86699

Browse files
committed
Test hydration for objects and functions
1 parent 39b142e commit 1c86699

2 files changed

Lines changed: 47 additions & 1 deletion

File tree

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,4 +558,42 @@ describe('ReactDOMServerHydration', () => {
558558
}
559559
});
560560
});
561+
562+
it('should not re-assign properties on hydration', () => {
563+
const container = document.createElement('div');
564+
document.body.appendChild(container);
565+
566+
const jsx = React.createElement('my-custom-element', {
567+
str: 'string',
568+
obj: {foo: 'bar'},
569+
});
570+
571+
container.innerHTML = ReactDOMServer.renderToString(jsx);
572+
const customElement = container.querySelector('my-custom-element');
573+
574+
// Install setters to activate `in` check
575+
Object.defineProperty(customElement, 'str', {
576+
set: function(x) {
577+
this._str = x;
578+
},
579+
get: function() {
580+
return this._str;
581+
},
582+
});
583+
Object.defineProperty(customElement, 'obj', {
584+
set: function(x) {
585+
this._obj = x;
586+
},
587+
get: function() {
588+
return this._obj;
589+
},
590+
});
591+
592+
ReactDOM.hydrate(jsx, container);
593+
594+
expect(customElement.getAttribute('str')).toBe('string');
595+
expect(customElement.getAttribute('obj')).toBe(null);
596+
expect(customElement.str).toBe(undefined);
597+
expect(customElement.obj).toBe(undefined);
598+
});
561599
});

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1110,7 +1110,15 @@ export function diffHydratedProperties(
11101110
serverValue = getValueForAttribute(domElement, propKey, nextProp);
11111111
}
11121112

1113-
if (nextProp !== serverValue && !isMismatchDueToBadCasing) {
1113+
const dontWarnCustomElement =
1114+
enableCustomElementPropertySupport &&
1115+
isCustomComponentTag &&
1116+
(typeof nextProp === 'function' || typeof nextProp === 'object');
1117+
if (
1118+
!dontWarnCustomElement &&
1119+
nextProp !== serverValue &&
1120+
!isMismatchDueToBadCasing
1121+
) {
11141122
warnForPropDifference(propKey, serverValue, nextProp);
11151123
}
11161124
}

0 commit comments

Comments
 (0)