mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
Warns when mutated props are passed.
This commit is contained in:
1 parent
761921499c
commit
593a234c1e
2 files changed
+39
No files matched your search
@@ -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' ||
|
||||
!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
|
||||
|
||||
@@ -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.'
|
||||
);
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
Reference in new issue
Block a user