mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
Revert "Don't mutate passed-in props"
This reverts https://github.com/facebook/react/pull/576 This approach mutates the default props for the instance on each update, which causes incorrect behavior. discussed with @balpert. can look into cloning but this unbreaks.
This commit is contained in:
committed by
Paul O’Shannessy
parent
a7f6082c9c
commit
12e765dd27
@@ -676,7 +676,7 @@ var ReactCompositeComponentMixin = {
|
||||
this._compositeLifeCycleState = CompositeLifeCycle.MOUNTING;
|
||||
|
||||
this._defaultProps = this.getDefaultProps ? this.getDefaultProps() : null;
|
||||
this.props = this._processProps(this.props);
|
||||
this._processProps(this.props);
|
||||
|
||||
if (this.__reactAutoBindMap) {
|
||||
this._bindAutoBindMethods();
|
||||
@@ -844,31 +844,22 @@ var ReactCompositeComponentMixin = {
|
||||
|
||||
/**
|
||||
* Processes props by setting default values for unspecified props and
|
||||
* asserting that the props are valid. Does not mutate its argument; returns
|
||||
* a new props object with defaults merged in.
|
||||
* asserting that the props are valid.
|
||||
*
|
||||
* @param {object} newProps
|
||||
* @return {object}
|
||||
* @param {object} props
|
||||
* @private
|
||||
*/
|
||||
_processProps: function(newProps) {
|
||||
var props = this._defaultProps;
|
||||
if (props) {
|
||||
// To avoid mutating the props that are passed into the constructor, we
|
||||
// copy onto the object returned by getDefaultProps instead.
|
||||
for (var propName in newProps) {
|
||||
if (typeof newProps[propName] !== 'undefined') {
|
||||
props[propName] = newProps[propName];
|
||||
}
|
||||
_processProps: function(props) {
|
||||
var defaultProps = this._defaultProps;
|
||||
for (var propName in defaultProps) {
|
||||
if (typeof props[propName] === 'undefined') {
|
||||
props[propName] = defaultProps[propName];
|
||||
}
|
||||
} else {
|
||||
props = newProps;
|
||||
}
|
||||
var propTypes = this.constructor.propTypes;
|
||||
if (propTypes) {
|
||||
this._checkPropTypes(propTypes, props, ReactPropTypeLocations.prop);
|
||||
}
|
||||
return props;
|
||||
},
|
||||
|
||||
/**
|
||||
@@ -920,7 +911,8 @@ var ReactCompositeComponentMixin = {
|
||||
|
||||
var nextProps = this.props;
|
||||
if (this._pendingProps != null) {
|
||||
nextProps = this._processProps(this._pendingProps);
|
||||
nextProps = this._pendingProps;
|
||||
this._processProps(nextProps);
|
||||
this._pendingProps = null;
|
||||
|
||||
this._compositeLifeCycleState = CompositeLifeCycle.RECEIVING_PROPS;
|
||||
|
||||
@@ -213,26 +213,6 @@ describe('ReactCompositeComponent', function() {
|
||||
reactComponentExpect(instance3).scalarPropsEqual({key: null});
|
||||
});
|
||||
|
||||
it('should not mutate passed-in props object', function() {
|
||||
var Component = React.createClass({
|
||||
getDefaultProps: function() {
|
||||
return {key: 'testKey'};
|
||||
},
|
||||
render: function() {
|
||||
return <span />;
|
||||
}
|
||||
});
|
||||
|
||||
var inputProps = {};
|
||||
var instance1 = Component(inputProps);
|
||||
ReactTestUtils.renderIntoDocument(instance1);
|
||||
expect(instance1.props.key).toBe('testKey');
|
||||
|
||||
// We don't mutate the input, just in case the caller wants to do something
|
||||
// with it after using it to instantiate a component
|
||||
expect(inputProps.key).not.toBeDefined();
|
||||
});
|
||||
|
||||
it('should normalize props with default values', function() {
|
||||
var Component = React.createClass({
|
||||
propTypes: {key: ReactPropTypes.string.isRequired},
|
||||
|
||||
Reference in New Issue
Block a user