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

Warns when mutated props are passed. #5346

Merged
merged 1 commit into from
Dec 17, 2015
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
12 changes: 12 additions & 0 deletions src/renderers/shared/reconciler/ReactCompositeComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,18 @@ var ReactCompositeComponentMixin = {
Component.displayName || Component.name || 'Component'
);
}

var propsMutated = inst.props !== publicProps;
var componentName =
Component.displayName || Component.name || 'Component';

warning(
typeof inst.props === 'undefined' ||
Copy link
Contributor

Choose a reason for hiding this comment

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

inst.props === undefined is simpler – but why not do this check in the React.Component constructor instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The same would have to be repeated at React.createClass right?

!propsMutated,
'%s(...): When calling super() in `%s`, make sure to pass ' +
'up the same props that your component\'s constructor was passed.',
componentName, componentName
);
}

// These should be set up in the constructor, but as a convenience for
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1241,4 +1241,31 @@ describe('ReactCompositeComponent', function() {

});

it('should warn when mutated props are passed', function() {

var container = document.createElement('div');

class Foo extends React.Component {
constructor(props) {
var _props = { idx: props.idx + '!' };
super(_props);
}

render() {
return <span />;
}
}

expect(console.error.calls.length).toBe(0);

ReactDOM.render(<Foo idx="qwe" />, container);

expect(console.error.calls.length).toBe(1);
expect(console.error.argsForCall[0][0]).toContain(
'Foo(...): When calling super() in `Foo`, make sure to pass ' +
'up the same props that your component\'s constructor was passed.'
);

});

});