Match-id-78aab772cb8e654ef883d9b69084d2c4da273119

This commit is contained in:
* 2022-09-06 21:50:47 +08:00 committed by *
parent e6e3c70bb1
commit 5bc66a4e69
3 changed files with 52 additions and 3 deletions

View File

@ -294,8 +294,9 @@ function diffArrayNodesHandler(
if (rightOldIndex < 0 || rightOldNode === null) { if (rightOldIndex < 0 || rightOldNode === null) {
break; break;
} }
// 新旧节点的index应该相同才能复用null会影响位置
canBeReuse = checkCanReuseNode(rightOldNode, newChildren[rightIdx - 1]); const samePosition = rightOldNode.eIndex === rightIdx - 1;
canBeReuse = checkCanReuseNode(rightOldNode, newChildren[rightIdx - 1]) && samePosition;
// 不能复用break // 不能复用break
if (!canBeReuse) { if (!canBeReuse) {
break; break;

View File

@ -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);
});
});

View File

@ -81,7 +81,7 @@ describe('useEffect Hook Test', () => {
expect(LogUtils.getAndClear()).toEqual([]); expect(LogUtils.getAndClear()).toEqual([]);
// 在执行新的render前会执行完上一次render的useEffect所以LogUtils会加入'NewApp effect'。 // 在执行新的render前会执行完上一次render的useEffect所以LogUtils会加入'NewApp effect'。
Horizon.render([na], container); Horizon.render([na], container);
expect(LogUtils.getAndClear()).toEqual(['NewApp effect']); expect(LogUtils.getAndClear()).toEqual(['NewApp effect', 'NewApp']);
expect(container.textContent).toBe('NewApp'); expect(container.textContent).toBe('NewApp');
expect(LogUtils.getAndClear()).toEqual([]); expect(LogUtils.getAndClear()).toEqual([]);
}); });