Preserve the original object when using replaceState

When we use only replace state we don't need to clone the object.
This commit is contained in:
Sebastian Markbage
2016-11-22 16:16:46 -08:00
parent a489e3f057
commit a481016883
3 changed files with 21 additions and 10 deletions
-1
View File
@@ -114,7 +114,6 @@ src/renderers/shared/stack/reconciler/__tests__/ReactCompositeComponent-test.js
* should warn about `setState` in render
* should warn about `setState` in getChildContext
* should update refs if shouldComponentUpdate gives false
* should support objects with prototypes as state
src/renderers/shared/stack/reconciler/__tests__/ReactCompositeComponentNestedState-test.js
* should provide up to date values for props
+1
View File
@@ -1284,6 +1284,7 @@ src/renderers/shared/stack/reconciler/__tests__/ReactCompositeComponent-test.js
* should allow access to findDOMNode in componentWillUnmount
* context should be passed down from the parent
* should replace state
* should support objects with prototypes as state
* should not warn about unmounting during unmounting
* should only call componentWillUnmount once
@@ -87,19 +87,30 @@ exports.callCallbacks = function(queue : UpdateQueue, context : any) : Error | n
return firstError;
};
function getStateFromNode(node, instance, state, props) {
if (typeof node.partialState === 'function') {
const updateFn = node.partialState;
return updateFn.call(instance, state, props);
} else {
return node.partialState;
}
}
exports.mergeUpdateQueue = function(queue : UpdateQueue, instance : any, prevState : any, props : any) : any {
let node : ?UpdateQueueNode = queue;
if (queue.isReplace) {
// replaceState is always first in the queue.
prevState = getStateFromNode(queue, instance, prevState, props);
node = queue.next;
if (!node) {
// If there is no more work, we replace the raw object instead of cloning.
return prevState;
}
}
let state = Object.assign({}, prevState);
while (node) {
state = node.isReplace ? null : state;
let partialState;
if (typeof node.partialState === 'function') {
const updateFn = node.partialState;
partialState = updateFn.call(instance, state, props);
} else {
partialState = node.partialState;
}
state = Object.assign(state || {}, partialState);
let partialState = getStateFromNode(node, instance, state, props);
Object.assign(state, partialState);
node = node.next;
}
return state;