Skip to content

Commit 1a093e5

Browse files
committed
Emit warning when assigning to read only properties in client
1 parent af292bc commit 1a093e5

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

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

+22
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,28 @@ describe('DOMPropertyOperations', () => {
289289
expect(customElement.getAttribute('textContent')).toBe(null);
290290
expect(customElement.hasChildNodes()).toBe(false);
291291
});
292+
293+
// @gate enableCustomElementPropertySupport
294+
it('Assigning to read-only properties should emit warnings for custom elements', () => {
295+
const readOnlyProperties = [
296+
'isContentEditable',
297+
'offsetParent',
298+
'offsetTop',
299+
'offsetLeft',
300+
'offsetWidth',
301+
'offsetHeight'
302+
];
303+
for (const readOnlyProperty of readOnlyProperties) {
304+
const container = document.createElement('div');
305+
const props = {};
306+
props[readOnlyProperty] = 'foo';
307+
expect(() => {
308+
ReactDOM.render(React.createElement('my-custom-element', props), container);
309+
}).toErrorDev(
310+
`Assigning to the read-only property \`${readOnlyProperty}\` won't have any effect on the element.`
311+
);
312+
}
313+
});
292314
});
293315

294316
describe('deleteValueForProperty', () => {

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

+14
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,20 @@ export function setValueForProperty(
176176
}
177177
}
178178

179+
if (__DEV__ && enableCustomElementPropertySupport && isCustomComponentTag) {
180+
const readOnlyProperties = [
181+
'isContentEditable',
182+
'offsetParent',
183+
'offsetTop',
184+
'offsetLeft',
185+
'offsetWidth',
186+
'offsetHeight'
187+
];
188+
if (readOnlyProperties.includes(name)) {
189+
console.error("Assigning to the read-only property `%s` won't have any effect on the element.", name);
190+
}
191+
}
192+
179193
if (shouldRemoveAttribute(name, value, propertyInfo, isCustomComponentTag)) {
180194
value = null;
181195
}

0 commit comments

Comments
 (0)