Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Warn on string "true" as well as "false" in DOM boolean props
  • Loading branch information
motiz88 committed Aug 12, 2018
commit f3248f2f87a6a887d01e797a347d78dcfcf7f26f
12 changes: 12 additions & 0 deletions packages/react-dom/src/__tests__/ReactDOMComponent-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2518,6 +2518,18 @@ describe('ReactDOMComponent', () => {

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.

'Did you mean hidden={true}?',
);

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

describe('Hyphenated SVG elements', function() {
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
7 changes: 4 additions & 3 deletions packages/react-dom/src/shared/ReactDOMUnknownPropertyHook.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,19 +227,20 @@ if (__DEV__) {
return false;
}

// Warn when passing the string 'false' into a boolean prop
// Warn when passing the strings 'false' or 'true' into a boolean prop
if (
value === 'false' &&
(value === 'false' || value === 'true') &&
propertyInfo !== null &&
propertyInfo.type === BOOLEAN
) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let’s make value === 'false' the first condition. Since it’s rare we won’t have to check most other conditions.

warning(
false,
'Received the string `%s` for the boolean attribute `%s`. ' +
'Did you mean %s={false}?',
'Did you mean %s={%s}?',
value,
name,
name,
value,
);
warnedProperties[name] = true;
return true;
Expand Down