Skip to content

fix(useVirtualList): 数据抖动 #1807

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/hooks/src/useFullscreen/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ const useFullscreen = (target: BasicTarget, options?: Options) => {

const onChange = () => {
if (screenfull.isEnabled) {
const { isFullscreen } = screenfull;
const el = getTargetElement(target);
if (!el) {
return;
}
const isFullscreen = document.fullscreenElement === el;
if (isFullscreen) {
onEnterRef.current?.();
} else {
Expand Down
29 changes: 16 additions & 13 deletions packages/hooks/src/useVirtualList/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useMemo, useState, useRef } from 'react';
import { useEffect, useMemo, useState, useRef, useLayoutEffect } from 'react';
import useEventListener from '../useEventListener';
import useLatest from '../useLatest';
import useMemoizedFn from '../useMemoizedFn';
Expand Down Expand Up @@ -27,6 +27,8 @@ const useVirtualList = <T = any>(list: T[], options: Options<T>) => {

const [targetList, setTargetList] = useState<{ index: number; data: T }[]>([]);

const [offsetTop, setOffsetTop] = useState<number>(0);

const getVisibleCount = (containerHeight: number, fromIndex: number) => {
if (isNumber(itemHeightRef.current)) {
return Math.ceil(containerHeight / itemHeightRef.current);
Expand Down Expand Up @@ -68,22 +70,23 @@ const useVirtualList = <T = any>(list: T[], options: Options<T>) => {
const height = index * itemHeightRef.current;
return height;
}
const height = list
.slice(0, index)
.reduce((sum, _, i) => sum + (itemHeightRef.current as ItemHeight<T>)(i, list[i]), 0);
const height = list.slice(0, index).reduce((sum, _, i) => sum + (itemHeightRef.current as ItemHeight<T>)(i, list[i]), 0);
return height;
};

const totalHeight = useMemo(() => {
if (isNumber(itemHeightRef.current)) {
return list.length * itemHeightRef.current;
}
return list.reduce(
(sum, _, index) => sum + (itemHeightRef.current as ItemHeight<T>)(index, list[index]),
0,
);
return list.reduce((sum, _, index) => sum + (itemHeightRef.current as ItemHeight<T>)(index, list[index]), 0);
}, [list]);

useLayoutEffect(() => {
const wrapper = getTargetElement(wrapperTarget) as HTMLElement;
wrapper.style.height = totalHeight - offsetTop + 'px';
wrapper.style.marginTop = offsetTop + 'px';
}, [totalHeight, wrapperTarget, offsetTop]);

const calculateRange = () => {
const container = getTargetElement(containerTarget);
const wrapper = getTargetElement(wrapperTarget) as HTMLElement;
Expand All @@ -98,15 +101,15 @@ const useVirtualList = <T = any>(list: T[], options: Options<T>) => {
const end = Math.min(list.length, offset + visibleCount + overscan);

const offsetTop = getDistanceTop(start);

wrapper.style.height = totalHeight - offsetTop + 'px';
wrapper.style.marginTop = offsetTop + 'px';
setOffsetTop(offsetTop);
// wrapper.style.height = totalHeight - offsetTop + 'px';
// wrapper.style.marginTop = offsetTop + 'px';

setTargetList(
list.slice(start, end).map((ele, index) => ({
data: ele,
index: index + start,
})),
}))
);
}
};
Expand All @@ -130,7 +133,7 @@ const useVirtualList = <T = any>(list: T[], options: Options<T>) => {
},
{
target: containerTarget,
},
}
);

const scrollTo = (index: number) => {
Expand Down