Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve prototype with replaceState #3841

Merged
merged 1 commit into from
May 8, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/core/ReactCompositeComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,10 @@ var ReactCompositeComponentMixin = {
return inst.state;
}

if (replace && queue.length === 1) {
return queue[0];
}

var nextState = assign({}, replace ? queue[0] : inst.state);
for (var i = replace ? 1 : 0; i < queue.length; i++) {
var partial = queue[i];
Expand Down
65 changes: 65 additions & 0 deletions src/core/__tests__/ReactCompositeComponent-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -877,4 +877,69 @@ describe('ReactCompositeComponent', function() {
expect(console.error.argsForCall.length).toBe(0);
});

it('should replace state', function() {
var Moo = React.createClass({
getInitialState: function() {
return {x: 1};
},
render: function() {
return <div />;
}
});

var moo = ReactTestUtils.renderIntoDocument(<Moo />);
moo.replaceState({y: 2});
expect('x' in moo.state).toBe(false);
expect(moo.state.y).toBe(2);
});

it('should support objects with prototypes as state', function() {
var NotActuallyImmutable = function(str) {
this.str = str;
};
NotActuallyImmutable.prototype.amIImmutable = function() {
return true;
};
var Moo = React.createClass({
getInitialState: function() {
return new NotActuallyImmutable('first');
},
render: function() {
return <div />;
}
});

var moo = ReactTestUtils.renderIntoDocument(<Moo />);
expect(moo.state.str).toBe('first');
expect(moo.state.amIImmutable()).toBe(true);

var secondState = new NotActuallyImmutable('second');
moo.replaceState(secondState);
expect(moo.state.str).toBe('second');
expect(moo.state.amIImmutable()).toBe(true);
expect(moo.state).toBe(secondState);

moo.setState({str: 'third'});
expect(moo.state.str).toBe('third');
// Here we lose the prototype.
expect(moo.state.amIImmutable).toBe(undefined);

// When more than one state update is enqueued, we have the same behavior
var fifthState = new NotActuallyImmutable('fifth');
ReactUpdates.batchedUpdates(function() {
moo.setState({str: 'fourth'});
moo.replaceState(fifthState);
});
expect(moo.state).toBe(fifthState);

// When more than one state update is enqueued, we have the same behavior
var sixthState = new NotActuallyImmutable('sixth');
ReactUpdates.batchedUpdates(function() {
moo.replaceState(sixthState);
moo.setState({str: 'seventh'});
});
expect(moo.state.str).toBe('seventh');
expect(moo.state.amIImmutable).toBe(undefined);
});

});