@samitouri / QOS-React-2 / commits / c341269ab2

simple fix to prevent overwriting of mutated props

Alex Boatwright committed Oct 28, 2015 at 23:37 UTC c341269ab28924af1901e6b559515690bde973a4
2 files changed +50 -1
src/renderers/shared/reconciler/ReactCompositeComponent.js
+1 -1
@@ -199,7 +199,7 @@ var ReactCompositeComponentMixin = {
199
200 // These should be set up in the constructor, but as a convenience for
201 // simpler class abstractions, we set them up after the fact.
202 - inst.props = publicProps;
202 + inst.props = inst.props || publicProps;
203 inst.context = publicContext;
204 inst.refs = emptyObject;
205 inst.updater = ReactUpdateQueue;
src/renderers/shared/reconciler/__tests__/ReactCompositeComponent-test.js
+49
@@ -1241,4 +1241,53 @@ describe('ReactCompositeComponent', function() {
1241
1242 });
1243
1244 + it('should not overwrite mutated properties', function() {
1245 + function Moo(props) {
1246 + React.Component.call(this, props);
1247 + }
1248 + Moo.prototype = Object.create(React.Component.prototype, {
1249 + constructor: {
1250 + value: Moo,
1251 + },
1252 + });
1253 + Moo.prototype.render = function() {
1254 + return <span />;
1255 + };
1256 + Moo.defaultProps = { idx: 'moo' };
1257 +
1258 + function Foo(props) {
1259 + var _props = { idx: this.constructor.defaultProps.idx + '?' };
1260 +
1261 + React.Component.call(this, _props);
1262 + }
1263 + Foo.prototype = Object.create(React.Component.prototype, {
1264 + constructor: {
1265 + value: Foo,
1266 + },
1267 + });
1268 + Foo.prototype.render = function() {
1269 + return <span />;
1270 + };
1271 + Foo.defaultProps = { idx: 'foo' };
1272 +
1273 + function Bar() {
1274 + Foo.call(this);
1275 + }
1276 +
1277 + Bar.prototype = Object.create(Foo.prototype, {
1278 + constructor: {
1279 + value: Bar,
1280 + },
1281 + });
1282 + Bar.defaultProps = { idx: 'bar' };
1283 +
1284 + var moo = ReactTestUtils.renderIntoDocument(<Moo />);
1285 + expect(moo.props.idx).toBe('moo');
1286 +
1287 + var foo = ReactTestUtils.renderIntoDocument(<Foo />);
1288 + expect(foo.props.idx).toBe('foo?');
1289 +
1290 + var bar = ReactTestUtils.renderIntoDocument(<Bar />);
1291 + expect(bar.props.idx).toBe('bar?');
1292 + });
1293 });