Skip to content

Warn when propTypes does not specify a received prop #2060

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

Closed
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
14 changes: 13 additions & 1 deletion src/core/ReactCompositeComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -987,7 +987,19 @@ var ReactCompositeComponentMixin = {
// TODO: Stop validating prop types here and only use the descriptor
// validation.
var componentName = this.constructor.displayName;
for (var propName in propTypes) {
var propName;

for (propName in props) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm glad we only do this in __DEV__!

if (props.hasOwnProperty(propName) &&
!propTypes.hasOwnProperty(propName)) {
var message = location.substring(0,1).toUpperCase() +
location.substring(1) + " `" + propName +
"` was not expected in " + ("`" + componentName + "`.");
warning(false, message);
}
}

for (propName in propTypes) {
if (propTypes.hasOwnProperty(propName)) {
var error =
propTypes[propName](props, propName, componentName, location);
Expand Down
17 changes: 16 additions & 1 deletion src/core/ReactDescriptorValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,22 @@ function validateChildKeys(component, parentType) {
* @private
*/
function checkPropTypes(componentName, propTypes, props, location) {
for (var propName in propTypes) {
var propName;
for (propName in props) {
if (props.hasOwnProperty(propName) &&
!propTypes.hasOwnProperty(propName)) {
var message = location.substring(0,1).toUpperCase() +
location.substring(1) + " `" + propName +
"` was not expected in " + ("`" + componentName + "`.");
loggedTypeFailures[message] = true;
monitorCodeUse(
'react_failed_descriptor_type_check',
{ message: message }
);
}
}

for (propName in propTypes) {
if (propTypes.hasOwnProperty(propName)) {
var error;
// Prop type validation may throw. In case they do, we don't want to
Expand Down
16 changes: 16 additions & 0 deletions src/core/__tests__/ReactCompositeComponent-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,22 @@ describe('ReactCompositeComponent', function() {
expect(console.warn.mock.calls.length).toBe(2);
});

it('should warn about unexpected props', function() {
var Component = React.createClass({
propTypes: {},
render: function() {
return <span>{this.props.prop}</span>;
}
});

ReactTestUtils.renderIntoDocument(<Component prop={42} />);

expect(console.warn.mock.calls.length).toBe(1);
expect(console.warn.mock.calls[0][0]).toBe(
'Warning: Prop `prop` was not expected in `Component`.'
);
});

it('should throw on invalid prop types', function() {
expect(function() {
React.createClass({
Expand Down