Skip to content

Commit 2c59076

Browse files
motiz88gaearon
authored andcommitted
Warn when "false" or "true" is the value of a boolean DOM prop (#13372)
* Warn when the string "false" is the value of a boolean DOM prop * Only warn on exact case match for "false" in DOM boolean props * Warn on string "true" as well as "false" in DOM boolean props * Clarify warnings on "true" / "false" values in DOM boolean props
1 parent 24b0ed7 commit 2c59076

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2506,6 +2506,34 @@ describe('ReactDOMComponent', () => {
25062506
});
25072507
});
25082508

2509+
describe('Boolean attributes', function() {
2510+
it('warns on the ambiguous string value "false"', function() {
2511+
let el;
2512+
expect(() => {
2513+
el = ReactTestUtils.renderIntoDocument(<div hidden="false" />);
2514+
}).toWarnDev(
2515+
'Received the string `false` for the boolean attribute `hidden`. ' +
2516+
'The browser will interpret it as a truthy value. ' +
2517+
'Did you mean hidden={false}?',
2518+
);
2519+
2520+
expect(el.getAttribute('hidden')).toBe('');
2521+
});
2522+
2523+
it('warns on the potentially-ambiguous string value "true"', function() {
2524+
let el;
2525+
expect(() => {
2526+
el = ReactTestUtils.renderIntoDocument(<div hidden="true" />);
2527+
}).toWarnDev(
2528+
'Received the string `true` for the boolean attribute `hidden`. ' +
2529+
'Although this works, it will not work as expected if you pass the string "false". ' +
2530+
'Did you mean hidden={true}?',
2531+
);
2532+
2533+
expect(el.getAttribute('hidden')).toBe('');
2534+
});
2535+
});
2536+
25092537
describe('Hyphenated SVG elements', function() {
25102538
it('the font-face element is not a custom element', function() {
25112539
let el;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ describe('ReactDOMInput', () => {
444444

445445
it('should take `defaultValue` when changing to uncontrolled input', () => {
446446
const node = ReactDOM.render(
447-
<input type="text" value="0" readOnly="true" />,
447+
<input type="text" value="0" readOnly={true} />,
448448
container,
449449
);
450450
expect(node.value).toBe('0');

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import warning from 'shared/warning';
1313

1414
import {
1515
ATTRIBUTE_NAME_CHAR,
16+
BOOLEAN,
1617
RESERVED,
1718
shouldRemoveAttributeWithWarning,
1819
getPropertyInfo,
@@ -226,6 +227,29 @@ if (__DEV__) {
226227
return false;
227228
}
228229

230+
// Warn when passing the strings 'false' or 'true' into a boolean prop
231+
if (
232+
(value === 'false' || value === 'true') &&
233+
propertyInfo !== null &&
234+
propertyInfo.type === BOOLEAN
235+
) {
236+
warning(
237+
false,
238+
'Received the string `%s` for the boolean attribute `%s`. ' +
239+
'%s ' +
240+
'Did you mean %s={%s}?',
241+
value,
242+
name,
243+
value === 'false'
244+
? 'The browser will interpret it as a truthy value.'
245+
: 'Although this works, it will not work as expected if you pass the string "false".',
246+
name,
247+
value,
248+
);
249+
warnedProperties[name] = true;
250+
return true;
251+
}
252+
229253
return true;
230254
};
231255
}

0 commit comments

Comments
 (0)