Infinite scroll and virtualization lag
Maintainer thường phản hồi trong vòng 1 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 35/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Cần làm rõ
- Mức độ hoạt động
- Đình trệ
- Công nghệ
- react, typescript
- Lĩnh vực
- frontend, performance
Hướng nghiên cứu
Bắt đầu với code CandidatesListTable và DataTable được cung cấp, tập trung vào thiết lập @tanstack/react-virtual, tableContainerRef và đường đi handleScroll của ScrollArea. Tái hiện lệnh gọi API infinite-scroll trong khi cuộn, sau đó xác định xem phần tử cuộn và hành vi virtualization có khớp với các ví dụ hay không. Hoàn thành khi việc cuộn mượt mà trong lúc fetch các trang và virtualization hoạt động.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Describe the bug
Iam using Shadcn ui tables which is built on top of the tanstack tables,
i need a infinite scroll which should be smooth -initially it was smooth (befor infinite scroll) until i add infinite scroll for pagination, there it is getting lag
at the time of the calling for the next page it is getting lag
and in case of virtualization- i didn't get why it is not virtualized i followed everything according to the examples given
it would be really help full if any one help me fix this.
`import React from "react";
import { useDispatch, useSelector } from "react-redux";
import Columns from "./Columns";
import DataTable from "../tables/DataTable";
import { useEffect } from "react";
import {
fetchCandidates,
getCandidates,
} from "../../features/candidates/candidateThunks";
import { useState } from "react";
import {
QueryClient,
QueryClientProvider,
keepPreviousData,
useInfiniteQuery,
} from "@tanstack/react-query";
import { extractValueFromUrl } from "../../features/common/staticUtils";
function CandidatesListTable() {
// useGetCandidates();
// const data = useSelector((state) => state.candidates.data);
const dispatch = useDispatch();
const { data, fetchNextPage, hasNextPage, isFetching, isLoading } =
useInfiniteQuery({
queryKey: [
"candidates",
// sorting, //refetch when sorting changes
],
queryFn: async ({pageParam=1}) => {
const fetchedData = await fetchCandidates({pageParam});
return fetchedData;
},
initialPageParam: 1,
getNextPageParam: (lastPage, pages) => {
const nextPage = extractValueFromUrl(lastPage.next, "page");
const next=Number(nextPage)
console.log(nextPage, "next page infy");
return next?next:undefined
},
refetchOnWindowFocus: false,
placeholderData: keepPreviousData,
});
// console.log(
// data,
// fetchNextPage,
// hasNextPage,
// isFetching,
// isLoading,
// "check for the infy"
// );
return (
<>
</>
);
}
export default CandidatesListTable;
import {
flexRender,
getCoreRowModel,
getFilteredRowModel,
useReactTable
} from "@tanstack/react-table";
import { useEffect } from "react";
import { ScrollArea } from "../ui/scroll-area.jsx";
import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from "../ui/table.jsx";
import { useVirtualizer } from "@tanstack/react-virtual";
import { useCallback, useMemo, useRef, useState } from "react";
import { useDispatch, useSelector } from "react-redux";
import useDebounce from "../../hooks/useDebounce.js";
import { cn } from "../../lib/utils.js";
import ViewSideBar from "../virtualTables/ViewSideBar.js";
import DataTableControlsAndFilters from "./DataTableControlsAndFilters.js";
function DataTable({ data, fetchNextPage, isFetching, isLoading, columns }) {
const dispatch = useDispatch();
const tableContainerRef = useRef(null);
const totalCount = data?.totalCount;
const isSiderOpen = useSelector((state) => state.virtualTables.isSiderOpen);
const debouncedIsHovered = useDebounce(isSiderOpen, 500);
const [open, setOpen] = useState(false);
const [container, setContainer] = useState(null);
//flatten the array of arrays from the useInfiniteQuery hook
const flatData = useMemo(
() => data?.pages?.flatMap((page) => page.results) ?? [],
[data]
);
useEffect(() => {
console.log(flatData, "flatdata");
}, [flatData]);
const totalDBRowCount = data?.pages?.[0]?.count ?? 0;
const totalFetched = flatData.length;
//called on scroll and possibly on mount to fetch more data as the user scrolls and reaches bottom of table
const fetchMoreOnBottomReached = useCallback(
(containerRefElement) => {
if (containerRefElement) {
const { scrollHeight, scrollTop, clientHeight } = containerRefElement;
//once the user has scrolled within 500px of the bottom of the table, fetch more data if we can
if (
scrollHeight - scrollTop - clientHeight < 1500 &&
!isFetching &&
totalFetched < totalDBRowCount
) {
console.log(
scrollHeight - scrollTop - clientHeight,
"check for the iop"
);
fetchNextPage();
}
}
},
[fetchNextPage, isFetching, totalFetched, totalDBRowCount]
);
useEffect(() => {
fetchMoreOnBottomReached(tableContainerRef.current);
}, []);
const table = useReactTable({
data: flatData,
columns,
// manualPagination: true,
// onSortingChange: setSorting,
// onColumnFiltersChange: setColumnFilters,
getCoreRowModel: getCoreRowModel(),
// getPaginationRowModel: getPaginationRowModel(),
// getSortedRowModel: getSortedRowModel(),
getFilteredRowModel: getFilteredRowModel(),
// manualSorting: true,
debugTable: true,
// onPaginationChange:(e)=>{
// console.log("onPaginationChange",e())
// }
// onColumnVisibilityChange: setColumnVisibility,
// // onRowSelectionChange: setRowSelection,
// state: {
// sorting,
// columnFilters,
// columnVisibility,
// rowSelection,
// },
});
const { rows } = table.getRowModel();
const virtualizer = useVirtualizer({
count: rows.length,
estimateSize: () => 33, //estimate row height for accurate scrollbar dragging
getScrollElement: () => tableContainerRef.current,
//measure dynamic row height, except in firefox because it measures table border height incorrectly
measureElement:
typeof window !== "undefined" &&
navigator.userAgent.indexOf("Firefox") === -1
? (element) => element?.getBoundingClientRect().height
: undefined,
overscan: 5,
});
console.log(virtualizer.getVirtualItems().length,'length')
return (
<div
className={cn(" h-[calc(100vh_-158px)] relative w-full flex flex-row")}
>
{" "}
{open && <ViewSideBar />}
<ScrollArea
handleScroll={fetchMoreOnBottomReached}
// ref={tableContainerRef}
className={cn(
`relative overflow-auto`,
`w-full`
// `h-[${virtualizer.getTotalSize()}px]`
)}
>
<div className="h-full w-full" ref={tableContainerRef}>
<Table style={{ display: "grid" }} className="h-full">
<TableHeader
style={{
display: "grid",
position: "sticky",
top: 0,
zIndex: 1,
}}
className="sticky top-0 bg-white z-10"
>
{table.getHeaderGroups().map((headerGroup) => (
<TableRow
style={{ display: "flex", width: "100%" }}
key={headerGroup.id}
>
{headerGroup.headers.map((header) => {
return (
<TableHead
style={{
display: "flex",
width: header.getSize(),
}}
key={header.id}
>
{header.isPlaceholder
? null
: flexRender(
header.column.columnDef.header,
header.getContext()
)}
</TableHead>
);
})}
</TableRow>
))}
</TableHeader>
<TableBody
style={{
display: "grid",
height: `${virtualizer.getTotalSize()}px`, //tells scrollbar how big the table is
position: "relative", //needed for absolute positioning of rows
}}
onScroll={(e) => console.log(e, "scrolllingf")}
>
{virtualizer.getVirtualItems()?.length > 0 ? (
<>
{virtualizer.getVirtualItems()?.map((virtualRow, index) => {
const row = rows[virtualRow.index];
const loader = virtualRow.index > rows?.length - 1;
return (
<TableRow
data-index={virtualRow.index} //needed for dynamic row height measurement
ref={(node) => virtualizer.measureElement(node)} //measure dynamic row height
key={row.id}
style={{
display: "flex",
position: "absolute",
transform: `translateY(${virtualRow.start}px)`, //this should always be a `style` as it changes on scroll
width: "100%",
}}
// key={row?.id}
// style={{
// // height: `${virtualRow.size}px`,
// transform: `translateY(${
// virtualRow.start - index * virtualRow.size
// }px)`,
// }}
data-state={row?.getIsSelected() && "selected"}
>
{row?.getVisibleCells().map((cell) => (
<TableCell
key={cell.id}
style={{
display: "flex",
width: cell.column.getSize(),
}}
>
{flexRender(
cell.column.columnDef.cell,
cell.getContext()
)}
</TableCell>
))}
</TableRow>
);
})}
{/* <Loader2
ref={ref}
className="react-table my-4 h-8 w-8 animate-spin"
/> */}
</>
) : (
<TableRow>
<TableCell
colSpan={columns.length}
className="h-24 text-center"
>
No results.
</TableCell>
</TableRow>
)}
</TableBody>
</Table>
</div>
</ScrollArea>
</div>
{/* <DataTableFooter table={table} /> */}
</div>
);
}
export default DataTable;
`
thank you
Your minimal, reproducible example
vs code
Steps to reproduce
in infinite scroll at the time of api call there has been a lag
Expected behavior
the scroll should be smooth
How often does this bug happen?
None
Screenshots or Videos
No response
Platform
linux
tanstack-virtual version
v3.5.0
TypeScript version
No response
Additional context
No response
Terms & Code of Conduct
- I agree to follow this project's Code of Conduct
- I understand that if my bug cannot be reliable reproduced in a debuggable environment, it will probably not be fixed and this issue may even be closed.
- Ngôn ngữ chính
- TypeScript
- Star
- 7.1k
- Fork
- 472
- Merge trung bình
- 1 ngày 2 giờ
- Pull request đã merge (30 ngày)
- 12
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của TanStack/virtual
-
measureElement caches 0 size for nodes that aren't attached yet (Vue: rows stack at start 0 and the list scrolls away on mount)Có thể đã có người làm @webn00b đã nhận 10 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
Maintainer thường phản hồi trong vòng 1 ngày
-
`scrollToIndex(last, { align: 'end' })` stays short of the end after a measured row grows (virtual-core ≥ 3.17.0)Có thể đã có người làm @minwookshin đã nhận 8 ngày trước. Đang mở
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 55/100
TanStack/virtual#1290 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
virtual-core: desktop Safari drops the prepend anchor write when history lands during the top rubber-band bounceCó thể đã có người làm Có pull request liên kết đang mở hoặc đã được merge. Đang mở
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 38/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 62/100
Maintainer thường phản hồi trong vòng 1 ngày
-
marko-virtual: scrollToEnd during an in-flight prepend strands the view one prepend above the bottom (chat-pretext e2e fails on CI)Có thể làm lại được Pull request cho issue này đã bị đóng mà không được merge. Đang mở
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 55/100
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của TanStack/virtual
Issue tương tự
-
triage
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
github/docs#46222 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
agent-ready area: config area: skills type: chore upstream: brain-kit
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 95/100
-
enhancement priority:low ready-for-dev
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
Maintainer thường phản hồi trong vòng 1 ngày
-
bug escritorio mapa
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
marcosferr/reporte-ciudadano#4 · 1 bình luận ·
-
area: material/sort gemini-triaged needs triage
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
angular/components#33933 ·
Maintainer thường phản hồi trong vòng 1 ngày