From 593a234c1ebef9a20bcb239badb9bb20018d4710 Mon Sep 17 00:00:00 2001 From: Manas Date: Sat, 31 Oct 2015 15:29:48 +0530 Subject: [PATCH] Warns when mutated props are passed. --- .../reconciler/ReactCompositeComponent.js | 12 +++++++++ .../__tests__/ReactCompositeComponent-test.js | 27 +++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/src/renderers/shared/reconciler/ReactCompositeComponent.js b/src/renderers/shared/reconciler/ReactCompositeComponent.js index 4cb8633c91b92..6dd3571872164 100644 --- a/src/renderers/shared/reconciler/ReactCompositeComponent.js +++ b/src/renderers/shared/reconciler/ReactCompositeComponent.js @@ -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 diff --git a/src/renderers/shared/reconciler/__tests__/ReactCompositeComponent-test.js b/src/renderers/shared/reconciler/__tests__/ReactCompositeComponent-test.js index 298c5a1be2277..91a1f445e27ee 100644 --- a/src/renderers/shared/reconciler/__tests__/ReactCompositeComponent-test.js +++ b/src/renderers/shared/reconciler/__tests__/ReactCompositeComponent-test.js @@ -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 ; + } + } + + expect(console.error.calls.length).toBe(0); + + ReactDOM.render(, 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.' + ); + + }); + });