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

Fixed invalid prop types error message to be more specific #11308

Merged
merged 22 commits into from
Oct 31, 2017
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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
5 changes: 3 additions & 2 deletions packages/react-dom/src/__tests__/ReactDOMAttribute-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ describe('ReactDOM unknown attribute', () => {
expectDev(
normalizeCodeLocInfo(console.error.calls.argsFor(0)[0]),
).toMatch(
'Warning: Received `true` for non-boolean attribute `unknown`. ' +
'If this is expected, cast the value to a string.\n' +
'Warning: If you intentionally tried to pass a boolean, pass it as a string ' +
'instead: `true`="`unknown`". If you mean to conditionally pass an ' +
'attribute, use a ternary expression: `true`={condition ? value : null}.\n' +
' in div (at **)',
);
expectDev(console.error.calls.count()).toBe(1);
Expand Down
16 changes: 12 additions & 4 deletions packages/react-dom/src/__tests__/ReactDOMComponent-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2042,7 +2042,9 @@ describe('ReactDOMComponent', () => {
expect(el.hasAttribute('whatever')).toBe(false);

expectDev(console.error.calls.argsFor(0)[0]).toContain(
'Warning: Received `true` for non-boolean attribute `whatever`',
'If you intentionally tried to pass a boolean, pass it as a string ' +
'instead: `true`="`whatever`". If you mean to conditionally pass an ' +
'attribute, use a ternary expression: `true`={condition ? value : null}.',
);
});

Expand All @@ -2055,7 +2057,9 @@ describe('ReactDOMComponent', () => {
expect(el.hasAttribute('whatever')).toBe(false);

expectDev(console.error.calls.argsFor(0)[0]).toContain(
'Warning: Received `true` for non-boolean attribute `whatever`',
'If you intentionally tried to pass a boolean, pass it as a string ' +
'instead: `true`="`whatever`". If you mean to conditionally pass an ' +
'attribute, use a ternary expression: `true`={condition ? value : null}.',
);
});

Expand Down Expand Up @@ -2230,7 +2234,9 @@ describe('ReactDOMComponent', () => {
expect(el.hasAttribute('whatever')).toBe(false);

expectDev(console.error.calls.argsFor(0)[0]).toContain(
'Warning: Received `true` for non-boolean attribute `whatever`.',
'If you intentionally tried to pass a boolean, pass it as a string ' +
'instead: `true`="`whatever`". If you mean to conditionally pass an ' +
'attribute, use a ternary expression: `true`={condition ? value : null}.',
);
});

Expand Down Expand Up @@ -2283,7 +2289,9 @@ describe('ReactDOMComponent', () => {

expectDev(console.error.calls.count()).toBe(1);
expectDev(console.error.calls.argsFor(0)[0]).toContain(
'Warning: Received `false` for non-boolean attribute `whatever`.',
'If you intentionally tried to pass a boolean, pass it as a string ' +
'instead: `false`="`whatever`". If you mean to conditionally pass an ' +
'attribute, use a ternary expression: `false`={condition ? value : null}.',
);
});
});
Expand Down
6 changes: 4 additions & 2 deletions packages/react-dom/src/shared/ReactDOMUnknownPropertyHook.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,12 @@ if (__DEV__) {
if (typeof value === 'boolean') {
warning(
DOMProperty.shouldAttributeAcceptBooleanValue(name),
'Received `%s` for non-boolean attribute `%s`. If this is expected, cast ' +
'the value to a string.%s',
'If you intentionally tried to pass a boolean, pass it as a string ' +
'instead: `%s`="`%s`". If you mean to conditionally pass an ' +
'attribute, use a ternary expression: `%s`={condition ? value : null}.%s',
Copy link
Collaborator

@sebmarkbage sebmarkbage Oct 21, 2017

Choose a reason for hiding this comment

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

If the value is true then this is very unlikely to happen because the problematic pattern is condition && value specifically. I can't think of a way where you'd end up with true for the same reason. Maybe we should have two separate error messages for true and false where this recommendation is only for the false case?

Maybe also call out that the problematic pattern is condition && value specifically, so that people know what's wrong and what to look for along with your proposed recommendation.

Also, the recommendation should be to pass undefined, not null. It doesn't really matter for DOM components, but if it is a custom component that is the difference between defaultProps working and not working. So it's better to have consistent advice.

value,
name,
value,
getStackAddendum(),
);
warnedProperties[name] = true;
Expand Down