Skip to content

Commit 2399b82

Browse files
author
Brian Vaughn
committed
Further refined error message
1 parent e0e2ab5 commit 2399b82

File tree

2 files changed

+44
-15
lines changed

2 files changed

+44
-15
lines changed

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

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,10 @@ describe('ReactDOMComponent', () => {
449449
it('should not add an empty src attribute', () => {
450450
const container = document.createElement('div');
451451
expect(() => ReactDOM.render(<img src="" />, container)).toErrorDev(
452-
'An empty string ("") was passed to the src attribute.',
452+
'An empty string ("") was passed to the src attribute. ' +
453+
'This may cause the browser to download the whole page again over the network. ' +
454+
'To fix this, either do not render the element at all ' +
455+
'or pass null to src instead of an empty string.',
453456
);
454457
const node = container.firstChild;
455458
expect(node.hasAttribute('src')).toBe(false);
@@ -458,15 +461,20 @@ describe('ReactDOMComponent', () => {
458461
expect(node.hasAttribute('src')).toBe(true);
459462

460463
expect(() => ReactDOM.render(<img src="" />, container)).toErrorDev(
461-
'An empty string ("") was passed to the src attribute.',
464+
'An empty string ("") was passed to the src attribute. ' +
465+
'This may cause the browser to download the whole page again over the network. ' +
466+
'To fix this, either do not render the element at all ' +
467+
'or pass null to src instead of an empty string.',
462468
);
463469
expect(node.hasAttribute('src')).toBe(false);
464470
});
465471

466472
it('should not add an empty href attribute', () => {
467473
const container = document.createElement('div');
468474
expect(() => ReactDOM.render(<link href="" />, container)).toErrorDev(
469-
'An empty string ("") was passed to the href attribute.',
475+
'An empty string ("") was passed to the href attribute. ' +
476+
'To fix this, either do not render the element at all ' +
477+
'or pass null to href instead of an empty string.',
470478
);
471479
const node = container.firstChild;
472480
expect(node.hasAttribute('href')).toBe(false);
@@ -475,15 +483,19 @@ describe('ReactDOMComponent', () => {
475483
expect(node.hasAttribute('href')).toBe(true);
476484

477485
expect(() => ReactDOM.render(<link href="" />, container)).toErrorDev(
478-
'An empty string ("") was passed to the href attribute.',
486+
'An empty string ("") was passed to the href attribute. ' +
487+
'To fix this, either do not render the element at all ' +
488+
'or pass null to href instead of an empty string.',
479489
);
480490
expect(node.hasAttribute('href')).toBe(false);
481491
});
482492

483493
it('should not add an empty action attribute', () => {
484494
const container = document.createElement('div');
485495
expect(() => ReactDOM.render(<form action="" />, container)).toErrorDev(
486-
'An empty string ("") was passed to the action attribute.',
496+
'An empty string ("") was passed to the action attribute. ' +
497+
'To fix this, either do not render the element at all ' +
498+
'or pass null to action instead of an empty string.',
487499
);
488500
const node = container.firstChild;
489501
expect(node.hasAttribute('action')).toBe(false);
@@ -492,7 +504,9 @@ describe('ReactDOMComponent', () => {
492504
expect(node.hasAttribute('action')).toBe(true);
493505

494506
expect(() => ReactDOM.render(<form action="" />, container)).toErrorDev(
495-
'An empty string ("") was passed to the action attribute.',
507+
'An empty string ("") was passed to the action attribute. ' +
508+
'To fix this, either do not render the element at all ' +
509+
'or pass null to action instead of an empty string.',
496510
);
497511
expect(node.hasAttribute('action')).toBe(false);
498512
});
@@ -502,7 +516,9 @@ describe('ReactDOMComponent', () => {
502516
expect(() =>
503517
ReactDOM.render(<button formAction="" />, container),
504518
).toErrorDev(
505-
'An empty string ("") was passed to the formAction attribute.',
519+
'An empty string ("") was passed to the formAction attribute. ' +
520+
'To fix this, either do not render the element at all ' +
521+
'or pass null to formAction instead of an empty string.',
506522
);
507523
const node = container.firstChild;
508524
expect(node.hasAttribute('formAction')).toBe(false);
@@ -513,7 +529,9 @@ describe('ReactDOMComponent', () => {
513529
expect(() =>
514530
ReactDOM.render(<button formAction="" />, container),
515531
).toErrorDev(
516-
'An empty string ("") was passed to the formAction attribute.',
532+
'An empty string ("") was passed to the formAction attribute. ' +
533+
'To fix this, either do not render the element at all ' +
534+
'or pass null to formAction instead of an empty string.',
517535
);
518536
expect(node.hasAttribute('formAction')).toBe(false);
519537
});

packages/react-dom/src/shared/DOMProperty.js

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -170,13 +170,24 @@ export function shouldRemoveAttribute(
170170
if (enableFilterEmptyStringAttributesDOM) {
171171
if (propertyInfo.removeEmptyString && value === '') {
172172
if (__DEV__) {
173-
console.error(
174-
'An empty string ("") was passed to the %s attribute. ' +
175-
'To fix this, either do not render the element at all ' +
176-
'or pass null to %s instead of an empty string.',
177-
name,
178-
name,
179-
);
173+
if (name === 'src') {
174+
console.error(
175+
'An empty string ("") was passed to the %s attribute. ' +
176+
'This may cause the browser to download the whole page again over the network. ' +
177+
'To fix this, either do not render the element at all ' +
178+
'or pass null to %s instead of an empty string.',
179+
name,
180+
name,
181+
);
182+
} else {
183+
console.error(
184+
'An empty string ("") was passed to the %s attribute. ' +
185+
'To fix this, either do not render the element at all ' +
186+
'or pass null to %s instead of an empty string.',
187+
name,
188+
name,
189+
);
190+
}
180191
}
181192
return true;
182193
}

0 commit comments

Comments
 (0)