Skip to content

Commit

Permalink
Merge pull request #5346 from prometheansacrifice/warn-immutable-props
Browse files Browse the repository at this point in the history
Warns when mutated props are passed.
  • Loading branch information
sophiebits committed Dec 17, 2015
2 parents c9c3c33 + 593a234 commit 963b3ca
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/renderers/shared/reconciler/ReactCompositeComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1171,4 +1171,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 963b3ca

Please sign in to comment.