Make shouldUpdateReactComponent key logic clearer

...and inline getComponentKey into traverseAllChildren.js.
This commit is contained in:
Ben Alpert
2013-12-25 22:25:20 -07:00
parent b0431a51ca
commit 6fae670d19
3 changed files with 18 additions and 43 deletions
-38
View File
@@ -1,38 +0,0 @@
/**
* Copyright 2013 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @providesModule getComponentKey
*/
"use strict";
/**
* Generate a key string that identifies a component within a set.
*
* @param {*} component A component that could contain a manual key.
* @param {number} index Index that is used if a manual key is not provided.
* @return {string}
* @internal
*/
function getComponentKey(component, index) {
if (component && component.props && component.props.key != null) {
// Explicit key
return '{' + component.props.key + '}';
}
// Implicit key determined by the index in the set
return '[' + index + ']';
}
module.exports = getComponentKey;
+2 -4
View File
@@ -19,8 +19,6 @@
"use strict";
var getComponentKey = require('getComponentKey');
/**
* Given a `prevComponent` and `nextComponent`, determines if `prevComponent`
* should be updated as opposed to being destroyed or replaced.
@@ -35,8 +33,8 @@ function shouldUpdateReactComponent(prevComponent, nextComponent) {
if (prevComponent && nextComponent &&
prevComponent.constructor === nextComponent.constructor) {
if (prevComponent._owner === nextComponent._owner && (
getComponentKey(prevComponent, /* index: */ 0) ===
getComponentKey(nextComponent, /* index: */ 0)
(prevComponent.props && prevComponent.props.key) ===
(nextComponent.props && nextComponent.props.key)
)) {
return true;
} else {
+16 -1
View File
@@ -20,7 +20,6 @@
var ReactTextComponent = require('ReactTextComponent');
var getComponentKey = require('getComponentKey');
var invariant = require('invariant');
/**
@@ -31,6 +30,22 @@ var invariant = require('invariant');
* });
*/
/**
* Generate a key string that identifies a component within a set.
*
* @param {*} component A component that could contain a manual key.
* @param {number} index Index that is used if a manual key is not provided.
* @return {string}
*/
function getComponentKey(component, index) {
if (component && component.props && component.props.key != null) {
// Explicit key
return '{' + component.props.key + '}';
}
// Implicit key determined by the index in the set
return '[' + index + ']';
}
/**
* @param {?*} children Children tree container.
* @param {!string} nameSoFar Name of the key path so far.