Skip to content

Commit d10b36f

Browse files
committed
fix sort confusion
1 parent 47d3f51 commit d10b36f

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

examples/dynamic.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ const App = React.createClass({
1919
}, {
2020
children: '依次进入4',
2121
key: 4,
22+
}, {
23+
children: '依次进入5',
24+
key: 5,
25+
}, {
26+
children: '依次进入6',
27+
key: 6,
2228
}],
2329
};
2430
},
@@ -69,13 +75,28 @@ const App = React.createClass({
6975
});
7076
this.setState({ items });
7177
},
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+
},
7292
render() {
7393
return (
7494
<div>
7595
<button onClick={this.add}>点击新增</button>
7696
<button onClick={this.addTwo}>点击新增两个</button>
7797
<button onClick={this.removeAll}>移出所有</button>
7898
<button onClick={this.removeAndAdd}>移出与添加</button>
99+
<button onClick={this.removeAndAddTow}>头尾添加与移出两个</button>
79100
<QueueAnim>
80101
{this.state.items.map((item) => <div key={item.key}>
81102
{item.children} <a href="#" onClick={this.remove.bind(this, item.key)}>删除</a>

src/utils.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export function mergeChildren(prev, next) {
2929
// the combined list
3030
const nextChildrenPending = {};
3131
let pendingChildren = [];
32+
let followChildrenKey;
3233
prev.forEach((c) => {
3334
if (!c) {
3435
return;
@@ -38,11 +39,14 @@ export function mergeChildren(prev, next) {
3839
nextChildrenPending[c.key] = pendingChildren;
3940
pendingChildren = [];
4041
}
42+
followChildrenKey = c.key;
4143
} else if (c.key) {
4244
pendingChildren.push(c);
4345
}
4446
});
45-
47+
if (!followChildrenKey) {
48+
ret = ret.concat(pendingChildren);
49+
}
4650
next.forEach((c) => {
4751
if (!c) {
4852
return;
@@ -51,13 +55,8 @@ export function mergeChildren(prev, next) {
5155
ret = ret.concat(nextChildrenPending[c.key]);
5256
}
5357
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);
6160
}
6261
});
6362

0 commit comments

Comments
 (0)