Skip to content

Commit

Permalink
Warns when mutated props are passed.
Browse files Browse the repository at this point in the history
  • Loading branch information
ManasJayanth committed Nov 12, 2015
1 parent 7619214 commit 04e9539
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/renderers/shared/reconciler/ReactCompositeComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var emptyObject = require('emptyObject');
var invariant = require('invariant');
var shouldUpdateReactComponent = require('shouldUpdateReactComponent');
var warning = require('warning');
var shallowEqual = require('shallowEqual');

function getDeclarationErrorAddendum(component) {
var owner = component._currentElement._owner || null;
Expand Down Expand Up @@ -195,6 +196,22 @@ var ReactCompositeComponentMixin = {
Component.displayName || Component.name || 'Component'
);
}

var instPropsNotReactElements = !ReactElement.isValidElement(inst.props);
var publicPropsNotReactElements = !ReactElement.isValidElement(publicProps);
var propsMutated = !shallowEqual(inst.props || {}, publicProps);
var componentName = Component.displayName || Component.name ||
'Component';

warning(
!instPropsNotReactElements ||
!publicPropsNotReactElements ||
componentName === 'StatelessComponent' ||
!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.'
);

});

});

0 comments on commit 04e9539

Please sign in to comment.