@samitouri / QOS-React-2 / commits / b581c8cfc7
Fork 0

The library for web and native user interfaces.

Always reassign _key for every pass

Currently we're mutating _key. Mutation here is fine, but it needs to be idempotent - which it's not. This is causing some issues. Instead I reassign the _key every time it passes through a flattening. This means that it's unique and stable for a single pass through a composite component. When it's repassed another level, it loses it previous identity and is rekeyed by it's new location. For auto-generated keys by index, this actually means it has the same semantics as before flattening. For explicit keys, it has the effect that keys need to be unique at every level. Regardless of how the key got there. Every component needs to ensure that it doesn't combine keys from two different sources that may collide. This is also inline with the old semantics but less intuitive in the new model.

CommitSyncScript committed Jun 4, 2013 at 12:15 UTC b581c8cfc784b74e74af80307a799cb4437f7845
3 files changed +74 -3
src/core/ReactComponent.js
+2 -2
@@ -72,7 +72,7 @@ function isEmptyChild(child) {
72
73 function assignKey(setKey, child, index) {
74 if (ReactComponent.isValidComponent(child)) {
75 - var key = child._key || child.props.key;
75 + var key = child.props.key;
76 if (__DEV__) {
77 if (!HAS_WARNED && !key) {
78 HAS_WARNED = true;
@@ -300,7 +300,7 @@ var ReactComponent = {
300
301 } else if (!isEmptyChild(child)) {
302
303 - if (ReactComponent.isValidComponent(child) && !child._key) {
303 + if (ReactComponent.isValidComponent(child)) {
304 // This is a static node and therefore safe to key by index.
305 // No warning necessary.
306 child._key = child.props.key || ('' + (i - 1));
src/core/__tests__/ReactIdentity-test.js
+71
@@ -80,4 +80,75 @@ describe('ReactIdentity', function() {
80 .toEqual('.reactRoot[0].:2.:chipmunk');
81 });
82
83 + it('should let restructured components retain their uniqueness', function() {
84 + var instance0 = <span />;
85 + var instance1 = <span />;
86 + var instance2 = <span />;
87 + var wrapped = <div>{instance0} {instance1}</div>;
88 + var unwrappedAndAdded =
89 + <div>
90 + {instance2}
91 + {wrapped.props.children[0]}
92 + {wrapped.props.children[1]}
93 + </div>;
94 +
95 + expect(function() {
96 +
97 + React.renderComponent(unwrappedAndAdded, document.createElement('div'));
98 +
99 + }).not.toThrow();
100 + });
101 +
102 + it('should retain keys during updates in composite components', function() {
103 +
104 + var TestComponent = React.createClass({
105 + render: function() {
106 + return <div>{this.props.children}</div>;
107 + }
108 + });
109 +
110 + var TestContainer = React.createClass({
111 +
112 + getInitialState: function() {
113 + return { swapped: false };
114 + },
115 +
116 + swap: function() {
117 + this.setState({ swapped: true });
118 + },
119 +
120 + render: function() {
121 + return (
122 + <TestComponent>
123 + {this.state.swapped ? this.props.second : this.props.first}
124 + {this.state.swapped ? this.props.first : this.props.second}
125 + </TestComponent>
126 + );
127 + }
128 +
129 + });
130 +
131 + var instance0 = <span key="A" />;
132 + var instance1 = <span key="B" />;
133 +
134 + var wrapped = <TestContainer first={instance0} second={instance1} />;
135 +
136 + React.renderComponent(wrapped, document.createElement('div'));
137 +
138 + var beforeKey = wrapped
139 + ._renderedComponent
140 + ._renderedComponent
141 + .props.children[0]._key;
142 +
143 + wrapped.swap();
144 +
145 + var afterKey = wrapped
146 + ._renderedComponent
147 + ._renderedComponent
148 + .props.children[0]._key;
149 +
150 + expect(beforeKey).not.toEqual(afterKey);
151 +
152 + });
153 +
154 });
src/utils/mapChildren.js
+1 -1
@@ -26,7 +26,7 @@ function mapChildren(children, mapFunction, context) {
26 var child = children[ii];
27 var key = child._key;
28 var mappedChild = mapFunction.call(context, child, key, ii);
29 - mappedChild._key = key;
29 + mappedChild.props.key = key;
30 mappedChildren.push(mappedChild);
31 }
32 return mappedChildren;