From 4b81de93d3733ecc5d4dcaab7efad2a5eef1937d Mon Sep 17 00:00:00 2001 From: CommitSyncScript Date: Tue, 4 Jun 2013 12:15:22 -0700 Subject: [PATCH] use key="foo" for all components flattenChildren was only using key when child.mountInContainerNode exists, which is defined on ReactCompositeComponent, and not ReactNativeComponent. This uses the isValidComponent() fn to see if we should use this key. --- src/core/__tests__/ReactIdentity-test.js | 83 ++++++++++++++++++++++++ src/utils/flattenChildren.js | 3 +- 2 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 src/core/__tests__/ReactIdentity-test.js diff --git a/src/core/__tests__/ReactIdentity-test.js b/src/core/__tests__/ReactIdentity-test.js new file mode 100644 index 0000000000..ecf71a8553 --- /dev/null +++ b/src/core/__tests__/ReactIdentity-test.js @@ -0,0 +1,83 @@ +/** + * @jsx React.DOM + * @emails react-core + */ + +"use strict"; + +var React; +var ReactTestUtils; +var reactComponentExpect; + +describe('ReactIdentity', function() { + + beforeEach(function() { + require('mock-modules').autoMockOff().dumpCache(); + React = require('React'); + ReactTestUtils = require('ReactTestUtils'); + reactComponentExpect = require('reactComponentExpect'); + }); + + it('should allow keyed objects to express identity', function() { + var instance = +
+ {{ + first:
, + second:
+ }} +
; + + React.renderComponent(instance, document.createElement('div')); + var node = instance.getDOMNode(); + reactComponentExpect(instance).toBeDOMComponentWithChildCount(2); + expect(node.childNodes[0].id).toEqual('.reactRoot[0].:0:first'); + expect(node.childNodes[1].id).toEqual('.reactRoot[0].:0:second'); + }); + + it('should allow key property to express identity', function() { + var instance = +
+
+
+
; + + React.renderComponent(instance, document.createElement('div')); + var node = instance.getDOMNode(); + reactComponentExpect(instance).toBeDOMComponentWithChildCount(2); + expect(node.childNodes[0].id).toEqual('.reactRoot[0].:apple'); + expect(node.childNodes[1].id).toEqual('.reactRoot[0].:banana'); + }); + + it('should use instance identity', function() { + + var Wrapper = React.createClass({ + render: function() { + return {this.props.children}; + } + }); + + var instance = +
+ + + +
; + + React.renderComponent(instance, document.createElement('div')); + var node = instance.getDOMNode(); + reactComponentExpect(instance).toBeDOMComponentWithChildCount(3); + expect(node.childNodes[0].id) + .toEqual('.reactRoot[0].:wrap1'); + expect(node.childNodes[0].firstChild.id) + .toEqual('.reactRoot[0].:wrap1.:squirrel'); + expect(node.childNodes[1].id) + .toEqual('.reactRoot[0].:wrap2'); + expect(node.childNodes[1].firstChild.id) + .toEqual('.reactRoot[0].:wrap2.:bunny'); + expect(node.childNodes[2].id) + .toEqual('.reactRoot[0].:2'); + expect(node.childNodes[2].firstChild.id) + .toEqual('.reactRoot[0].:2.:chipmunk'); + }); + +}); diff --git a/src/utils/flattenChildren.js b/src/utils/flattenChildren.js index f7a4bd3372..031b86e588 100644 --- a/src/utils/flattenChildren.js +++ b/src/utils/flattenChildren.js @@ -51,8 +51,7 @@ var flattenChildrenImpl = function(res, children, nameSoFar) { if (Array.isArray(children)) { for (var i = 0; i < children.length; i++) { var child = children[i]; - key = child && child.mountInContainerNode && - (child._key || child.props.key); + key = child && (child._key || (child.props && child.props.key)); escapedKey = key ? escapeTextForBrowser(key) : ('' + i); flattenChildrenImpl( res,