Match-id-78aab772cb8e654ef883d9b69084d2c4da273119
This commit is contained in:
parent
e6e3c70bb1
commit
5bc66a4e69
|
@ -294,8 +294,9 @@ function diffArrayNodesHandler(
|
|||
if (rightOldIndex < 0 || rightOldNode === null) {
|
||||
break;
|
||||
}
|
||||
|
||||
canBeReuse = checkCanReuseNode(rightOldNode, newChildren[rightIdx - 1]);
|
||||
// 新旧节点的index应该相同才能复用,null会影响位置
|
||||
const samePosition = rightOldNode.eIndex === rightIdx - 1;
|
||||
canBeReuse = checkCanReuseNode(rightOldNode, newChildren[rightIdx - 1]) && samePosition;
|
||||
// 不能复用,break
|
||||
if (!canBeReuse) {
|
||||
break;
|
||||
|
|
|
@ -0,0 +1,48 @@
|
|||
/*
|
||||
* Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
|
||||
*/
|
||||
|
||||
import * as Horizon from '@cloudsop/horizon/index.ts';
|
||||
|
||||
describe('Diff Algorithm', () => {
|
||||
it('null should diff correctly', () => {
|
||||
const fn = jest.fn();
|
||||
|
||||
class C extends Horizon.Component {
|
||||
constructor() {
|
||||
super();
|
||||
fn();
|
||||
}
|
||||
|
||||
render() {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
let update;
|
||||
|
||||
function App() {
|
||||
const [current, setCurrent] = Horizon.useState(1);
|
||||
update = setCurrent;
|
||||
return (
|
||||
<>
|
||||
{current === 1 ? <C /> : null}
|
||||
{current === 2 ? <C /> : null}
|
||||
{current === 3 ? <C /> : null}
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
Horizon.render(<App text="app" />, container);
|
||||
expect(fn).toHaveBeenCalledTimes(1);
|
||||
|
||||
update(2);
|
||||
expect(fn).toHaveBeenCalledTimes(2);
|
||||
|
||||
update(3);
|
||||
expect(fn).toHaveBeenCalledTimes(3);
|
||||
|
||||
update(1);
|
||||
expect(fn).toHaveBeenCalledTimes(4);
|
||||
});
|
||||
});
|
|
@ -81,7 +81,7 @@ describe('useEffect Hook Test', () => {
|
|||
expect(LogUtils.getAndClear()).toEqual([]);
|
||||
// 在执行新的render前,会执行完上一次render的useEffect,所以LogUtils会加入'NewApp effect'。
|
||||
Horizon.render([na], container);
|
||||
expect(LogUtils.getAndClear()).toEqual(['NewApp effect']);
|
||||
expect(LogUtils.getAndClear()).toEqual(['NewApp effect', 'NewApp']);
|
||||
expect(container.textContent).toBe('NewApp');
|
||||
expect(LogUtils.getAndClear()).toEqual([]);
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue