Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warn when "false" or "true" is the value of a boolean DOM prop #13372

Merged
merged 4 commits into from
Aug 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions packages/react-dom/src/__tests__/ReactDOMComponent-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2506,6 +2506,34 @@ describe('ReactDOMComponent', () => {
});
});

describe('Boolean attributes', function() {
it('warns on the ambiguous string value "false"', function() {
let el;
expect(() => {
el = ReactTestUtils.renderIntoDocument(<div hidden="false" />);
}).toWarnDev(
'Received the string `false` for the boolean attribute `hidden`. ' +
musakarakas marked this conversation as resolved.
Show resolved Hide resolved
'The browser will interpret it as a truthy value. ' +
'Did you mean hidden={false}?',
);

expect(el.getAttribute('hidden')).toBe('');
});

it('warns on the potentially-ambiguous string value "true"', function() {
let el;
expect(() => {
el = ReactTestUtils.renderIntoDocument(<div hidden="true" />);
}).toWarnDev(
'Received the string `true` for the boolean attribute `hidden`. ' +
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add Although this works, it will not work as expected if you pass the string "false". to the true case.

'Although this works, it will not work as expected if you pass the string "false". ' +
'Did you mean hidden={true}?',
);

expect(el.getAttribute('hidden')).toBe('');
});
});

describe('Hyphenated SVG elements', function() {
it('the font-face element is not a custom element', function() {
let el;
Expand Down
2 changes: 1 addition & 1 deletion packages/react-dom/src/__tests__/ReactDOMInput-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ describe('ReactDOMInput', () => {

it('should take `defaultValue` when changing to uncontrolled input', () => {
const node = ReactDOM.render(
<input type="text" value="0" readOnly="true" />,
<input type="text" value="0" readOnly={true} />,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test was broken by adding the warning on "true". I opted to just fix what the warning was complaining about, which doesn't change the semantics of this test AFAICT.

container,
);
expect(node.value).toBe('0');
Expand Down
24 changes: 24 additions & 0 deletions packages/react-dom/src/shared/ReactDOMUnknownPropertyHook.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import warning from 'shared/warning';

import {
ATTRIBUTE_NAME_CHAR,
BOOLEAN,
RESERVED,
shouldRemoveAttributeWithWarning,
getPropertyInfo,
Expand Down Expand Up @@ -226,6 +227,29 @@ if (__DEV__) {
return false;
}

// Warn when passing the strings 'false' or 'true' into a boolean prop
if (
(value === 'false' || value === 'true') &&
propertyInfo !== null &&
propertyInfo.type === BOOLEAN
) {
warning(
false,
'Received the string `%s` for the boolean attribute `%s`. ' +
'%s ' +
'Did you mean %s={%s}?',
value,
name,
value === 'false'
? 'The browser will interpret it as a truthy value.'
: 'Although this works, it will not work as expected if you pass the string "false".',
name,
value,
);
warnedProperties[name] = true;
return true;
}

return true;
};
}
Expand Down