@samitouri / QOS-React-2 / commits / 6fae670d19

Make shouldUpdateReactComponent key logic clearer

...and inline getComponentKey into traverseAllChildren.js.

Ben Alpert committed Dec 25, 2013 at 22:22 UTC 6fae670d192ee83a47d1e19b6502f67b17f6a8b3
3 files changed +18 -43
src/core/getComponentKey.js deleted
-38
@@ -1,38 +0,0 @@
1 -/**
2 - * Copyright 2013 Facebook, Inc.
3 - *
4 - * Licensed under the Apache License, Version 2.0 (the "License");
5 - * you may not use this file except in compliance with the License.
6 - * You may obtain a copy of the License at
7 - *
8 - * http://www.apache.org/licenses/LICENSE-2.0
9 - *
10 - * Unless required by applicable law or agreed to in writing, software
11 - * distributed under the License is distributed on an "AS IS" BASIS,
12 - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 - * See the License for the specific language governing permissions and
14 - * limitations under the License.
15 - *
16 - * @providesModule getComponentKey
17 - */
18 -
19 -"use strict";
20 -
21 -/**
22 - * Generate a key string that identifies a component within a set.
23 - *
24 - * @param {*} component A component that could contain a manual key.
25 - * @param {number} index Index that is used if a manual key is not provided.
26 - * @return {string}
27 - * @internal
28 - */
29 -function getComponentKey(component, index) {
30 - if (component && component.props && component.props.key != null) {
31 - // Explicit key
32 - return '{' + component.props.key + '}';
33 - }
34 - // Implicit key determined by the index in the set
35 - return '[' + index + ']';
36 -}
37 -
38 -module.exports = getComponentKey;
src/core/shouldUpdateReactComponent.js
+2 -4
@@ -19,8 +19,6 @@
19
20 "use strict";
21
22 -var getComponentKey = require('getComponentKey');
23 -
22 /**
23 * Given a `prevComponent` and `nextComponent`, determines if `prevComponent`
24 * should be updated as opposed to being destroyed or replaced.
@@ -35,8 +33,8 @@ function shouldUpdateReactComponent(prevComponent, nextComponent) {
33 if (prevComponent && nextComponent &&
34 prevComponent.constructor === nextComponent.constructor) {
35 if (prevComponent._owner === nextComponent._owner && (
38 - getComponentKey(prevComponent, /* index: */ 0) ===
39 - getComponentKey(nextComponent, /* index: */ 0)
36 + (prevComponent.props && prevComponent.props.key) ===
37 + (nextComponent.props && nextComponent.props.key)
38 )) {
39 return true;
40 } else {
src/utils/traverseAllChildren.js
+16 -1
@@ -20,7 +20,6 @@
20
21 var ReactTextComponent = require('ReactTextComponent');
22
23 -var getComponentKey = require('getComponentKey');
23 var invariant = require('invariant');
24
25 /**
@@ -31,6 +30,22 @@ var invariant = require('invariant');
30 * });
31 */
32
33 +/**
34 + * Generate a key string that identifies a component within a set.
35 + *
36 + * @param {*} component A component that could contain a manual key.
37 + * @param {number} index Index that is used if a manual key is not provided.
38 + * @return {string}
39 + */
40 +function getComponentKey(component, index) {
41 + if (component && component.props && component.props.key != null) {
42 + // Explicit key
43 + return '{' + component.props.key + '}';
44 + }
45 + // Implicit key determined by the index in the set
46 + return '[' + index + ']';
47 +}
48 +
49 /**
50 * @param {?*} children Children tree container.
51 * @param {!string} nameSoFar Name of the key path so far.