@@ -2220,9 +2220,12 @@ export function attach(
2220
2220
2221
2221
const isProfilingSupported = false ; // TODO: Support Tree Base Duration Based on Children.
2222
2222
2223
- const key = null ; // TODO: Track keys on ReactComponentInfo;
2224
- const env = instance . data . env ;
2225
- let displayName = instance . data . name || '' ;
2223
+ const componentInfo = instance . data ;
2224
+
2225
+ const key =
2226
+ typeof componentInfo . key === 'string' ? componentInfo . key : null ;
2227
+ const env = componentInfo . env ;
2228
+ let displayName = componentInfo . name || '' ;
2226
2229
if ( typeof env === 'string' ) {
2227
2230
// We model environment as an HoC name for now.
2228
2231
displayName = env + '(' + displayName + ')' ;
@@ -2855,19 +2858,32 @@ export function attach(
2855
2858
) ;
2856
2859
}
2857
2860
}
2858
- const firstRemainingChild = remainingReconcilingChildren ;
2861
+ // TODO: Find the best matching existing child based on the key if defined.
2862
+
2863
+ let bestMatch = remainingReconcilingChildren ;
2864
+ if ( componentInfo . key != null ) {
2865
+ // If there is a key try to find a matching key in the set.
2866
+ bestMatch = remainingReconcilingChildren ;
2867
+ while ( bestMatch !== null ) {
2868
+ if ( bestMatch . kind === VIRTUAL_INSTANCE && bestMatch . data . key === componentInfo . key ) {
2869
+ break ;
2870
+ }
2871
+ bestMatch = bestMatch . nextSibling ;
2872
+ }
2873
+ }
2859
2874
if (
2860
- firstRemainingChild !== null &&
2861
- firstRemainingChild . kind === VIRTUAL_INSTANCE &&
2862
- firstRemainingChild . data . name === componentInfo . name &&
2863
- firstRemainingChild . data . env === componentInfo . env
2875
+ bestMatch !== null &&
2876
+ bestMatch . kind === VIRTUAL_INSTANCE &&
2877
+ bestMatch . data . name === componentInfo . name &&
2878
+ bestMatch . data . env === componentInfo . env &&
2879
+ bestMatch . data . key === componentInfo . key
2864
2880
) {
2865
2881
// If the previous children had a virtual instance in the same slot
2866
2882
// with the same name, then we claim it and reuse it for this update.
2867
2883
// Update it with the latest entry.
2868
- firstRemainingChild . data = componentInfo ;
2869
- moveChild ( firstRemainingChild ) ;
2870
- previousVirtualInstance = firstRemainingChild ;
2884
+ bestMatch . data = componentInfo ;
2885
+ moveChild ( bestMatch ) ;
2886
+ previousVirtualInstance = bestMatch ;
2871
2887
previousVirtualInstanceWasMount = false ;
2872
2888
} else {
2873
2889
// Otherwise we create a new instance.
@@ -4321,11 +4337,13 @@ export function attach(
4321
4337
) : InspectedElement | null {
4322
4338
const canViewSource = false ;
4323
4339
4324
- const key = null ; // TODO: Track keys on ReactComponentInfo;
4340
+ const componentInfo = virtualInstance . data ;
4341
+ const key =
4342
+ typeof componentInfo . key === 'string' ? componentInfo . key : null ;
4325
4343
const props = null ; // TODO: Track props on ReactComponentInfo;
4326
4344
4327
- const env = virtualInstance . data . env ;
4328
- let displayName = virtualInstance . data . name || '' ;
4345
+ const env = componentInfo . env ;
4346
+ let displayName = componentInfo . name || '' ;
4329
4347
if ( typeof env === 'string' ) {
4330
4348
// We model environment as an HoC name for now.
4331
4349
displayName = env + '(' + displayName + ')' ;
@@ -4384,7 +4402,7 @@ export function attach(
4384
4402
// Does the component have legacy context attached to it.
4385
4403
hasLegacyContext : false ,
4386
4404
4387
- key : key != null ? key : null ,
4405
+ key : key ,
4388
4406
4389
4407
displayName : displayName ,
4390
4408
type : ElementTypeVirtual ,
0 commit comments