Clone on mount

This is the first step towards descriptors. This will start cloning the
component when it's mounted instead of mounting the first instance.

This avoids an issue where a reference to the first instance can hang around
in props. Since a mounted component gets mutated, the descriptor changes.

We don't need to clone the props object itself. Mutating the shallow props
object of a child that's passed into you is already flawed. Those cases need to
use cloneWithProps. A props object is considered shallow frozen after it leaves
the render it was created in.
This commit is contained in:
Sebastian Markbage
2014-03-10 15:26:31 -07:00
committed by Paul O’Shannessy
parent 2ca810fbf3
commit 3ea3274ca4
11 changed files with 312 additions and 102 deletions
+17 -14
View File
@@ -20,33 +20,36 @@
"use strict";
/**
* Given a `prevComponent` and `nextComponent`, determines if `prevComponent`
* should be updated as opposed to being destroyed or replaced.
* Given a `prevComponentInstance` and `nextComponent`, determines if
* `prevComponentInstance` should be updated as opposed to being destroyed or
* replaced by a new instance. The second argument is a descriptor. Future
* versions of the reconciler should only compare descriptors to other
* descriptors.
*
* @param {?object} prevComponent
* @param {?object} nextComponent
* @return {boolean} True if `prevComponent` should be updated.
* @param {?object} prevComponentInstance
* @param {?object} nextDescriptor
* @return {boolean} True if `prevComponentInstance` should be updated.
* @protected
*/
function shouldUpdateReactComponent(prevComponent, nextComponent) {
function shouldUpdateReactComponent(prevComponentInstance, nextDescriptor) {
// TODO: Remove warning after a release.
if (prevComponent && nextComponent &&
prevComponent.constructor === nextComponent.constructor && (
(prevComponent.props && prevComponent.props.key) ===
(nextComponent.props && nextComponent.props.key)
if (prevComponentInstance && nextDescriptor &&
prevComponentInstance.constructor === nextDescriptor.constructor && (
(prevComponentInstance.props && prevComponentInstance.props.key) ===
(nextDescriptor.props && nextDescriptor.props.key)
)) {
if (prevComponent._owner === nextComponent._owner) {
if (prevComponentInstance._owner === nextDescriptor._owner) {
return true;
} else {
if (__DEV__) {
if (prevComponent.state) {
if (prevComponentInstance.state) {
console.warn(
'A recent change to React has been found to impact your code. ' +
'A mounted component will now be unmounted and replaced by a ' +
'component (of the same class) if their owners are different. ' +
'Previously, ownership was not considered when updating.',
prevComponent,
nextComponent
prevComponentInstance,
nextDescriptor
);
}
}