File tree Expand file tree Collapse file tree 2 files changed +28
-8
lines changed Expand file tree Collapse file tree 2 files changed +28
-8
lines changed Original file line number Diff line number Diff line change @@ -19,6 +19,12 @@ const App = React.createClass({
19
19
} , {
20
20
children : '依次进入4' ,
21
21
key : 4 ,
22
+ } , {
23
+ children : '依次进入5' ,
24
+ key : 5 ,
25
+ } , {
26
+ children : '依次进入6' ,
27
+ key : 6 ,
22
28
} ] ,
23
29
} ;
24
30
} ,
@@ -69,13 +75,28 @@ const App = React.createClass({
69
75
} ) ;
70
76
this . setState ( { items } ) ;
71
77
} ,
78
+ removeAndAddTow ( ) {
79
+ const items = this . state . items ;
80
+ items . splice ( items . length - 1 , 1 ) ;
81
+ items . splice ( items . length - 2 , 1 ) ;
82
+ items . push ( {
83
+ children : `新节点${ Date . now ( ) } ` ,
84
+ key : this . index ++ ,
85
+ } ) ;
86
+ items . unshift ( {
87
+ children : `新节点${ Date . now ( ) } -top` ,
88
+ key : this . index ++ ,
89
+ } ) ;
90
+ this . setState ( { items } ) ;
91
+ } ,
72
92
render ( ) {
73
93
return (
74
94
< div >
75
95
< button onClick = { this . add } > 点击新增</ button >
76
96
< button onClick = { this . addTwo } > 点击新增两个</ button >
77
97
< button onClick = { this . removeAll } > 移出所有</ button >
78
98
< button onClick = { this . removeAndAdd } > 移出与添加</ button >
99
+ < button onClick = { this . removeAndAddTow } > 头尾添加与移出两个</ button >
79
100
< QueueAnim >
80
101
{ this . state . items . map ( ( item ) => < div key = { item . key } >
81
102
{ item . children } < a href = "#" onClick = { this . remove . bind ( this , item . key ) } > 删除</ a >
Original file line number Diff line number Diff line change @@ -29,6 +29,7 @@ export function mergeChildren(prev, next) {
29
29
// the combined list
30
30
const nextChildrenPending = { } ;
31
31
let pendingChildren = [ ] ;
32
+ let followChildrenKey ;
32
33
prev . forEach ( ( c ) => {
33
34
if ( ! c ) {
34
35
return ;
@@ -38,11 +39,14 @@ export function mergeChildren(prev, next) {
38
39
nextChildrenPending [ c . key ] = pendingChildren ;
39
40
pendingChildren = [ ] ;
40
41
}
42
+ followChildrenKey = c . key ;
41
43
} else if ( c . key ) {
42
44
pendingChildren . push ( c ) ;
43
45
}
44
46
} ) ;
45
-
47
+ if ( ! followChildrenKey ) {
48
+ ret = ret . concat ( pendingChildren ) ;
49
+ }
46
50
next . forEach ( ( c ) => {
47
51
if ( ! c ) {
48
52
return ;
@@ -51,13 +55,8 @@ export function mergeChildren(prev, next) {
51
55
ret = ret . concat ( nextChildrenPending [ c . key ] ) ;
52
56
}
53
57
ret . push ( c ) ;
54
- } ) ;
55
-
56
- // 保持原有的顺序
57
- pendingChildren . forEach ( ( c ) => {
58
- const originIndex = prev . indexOf ( c ) ;
59
- if ( originIndex >= 0 ) {
60
- ret . splice ( originIndex , 0 , c ) ;
58
+ if ( c . key === followChildrenKey ) {
59
+ ret = ret . concat ( pendingChildren ) ;
61
60
}
62
61
} ) ;
63
62
You can’t perform that action at this time.
0 commit comments