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

Deduplicate "unknown prop" warning to ease pain of version updates #9488

Closed
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
15 changes: 15 additions & 0 deletions src/renderers/dom/shared/__tests__/ReactDOMComponent-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,21 @@ describe('ReactDOMComponent', () => {
);
});

it('should only warn for the first unknown props passed', () => {
spyOn(console, 'error');
var container = document.createElement('div');
ReactDOM.render(
// the 'span' should not trigger a second warning in React v15.6.0
<div foo="bar" baz="qux"><span hello="world" /></div>,
container,
);
expect(console.error.calls.count(0)).toBe(1);
expect(normalizeCodeLocInfo(console.error.calls.argsFor(0)[0])).toBe(
'Warning: Unknown props `foo`, `baz` on <div> tag. Remove these props from the element. ' +
'For details, see https://fb.me/react-unknown-prop\n in div (at **)'
);
});

it('should warn for onDblClick prop', () => {
spyOn(console, 'error');
var container = document.createElement('div');
Expand Down
8 changes: 8 additions & 0 deletions src/renderers/dom/shared/hooks/ReactDOMUnknownPropertyHook.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,13 @@ if (__DEV__) {
};
}

let alreadyWarned = false;

var warnUnknownProperties = function(debugID, element) {
if (alreadyWarned) {
return;
}

var unknownProps = [];
for (var key in element.props) {
var isValid = validateProperty(element.type, key, debugID);
Expand All @@ -117,6 +123,7 @@ var warnUnknownProperties = function(debugID, element) {
element.type,
ReactComponentTreeHook.getStackAddendumByID(debugID)
);
alreadyWarned = true;
} else if (unknownProps.length > 1) {
warning(
false,
Expand All @@ -126,6 +133,7 @@ var warnUnknownProperties = function(debugID, element) {
element.type,
ReactComponentTreeHook.getStackAddendumByID(debugID)
);
alreadyWarned = true;
}
};

Expand Down