fix virtualization occasionally not detecting scroll

This commit is contained in:
Sidney Alcantara
2022-11-21 17:16:32 +11:00
parent 763c0a20d0
commit 9829fdebc1
2 changed files with 20 additions and 3 deletions

View File

@@ -1,4 +1,5 @@
import { useMemo, useRef, useState, useEffect, useCallback } from "react";
import useStateRef from "react-usestateref";
import { useAtom, useSetAtom } from "jotai";
import { useThrottledCallback } from "use-debounce";
import {
@@ -92,7 +93,11 @@ export default function Table({
const updateColumn = useSetAtom(updateColumnAtom, tableScope);
const containerRef = useRef<HTMLDivElement>(null);
// Store a **state** and reference to the container element
// so the state can re-render `TableBody`, preventing virtualization
// not detecting scroll if the container element was initially `null`
const [containerEl, setContainerEl, containerRef] =
useStateRef<HTMLDivElement | null>(null);
const gridRef = useRef<HTMLDivElement>(null);
// Get column defs from table schema
@@ -205,11 +210,16 @@ export default function Table({
// for large screen heights
useEffect(() => {
fetchMoreOnBottomReached(containerRef.current);
}, [fetchMoreOnBottomReached, tablePage, tableNextPage.loading]);
}, [
fetchMoreOnBottomReached,
tablePage,
tableNextPage.loading,
containerRef,
]);
return (
<div
ref={containerRef}
ref={(el) => setContainerEl(el)}
onScroll={(e) => fetchMoreOnBottomReached(e.target as HTMLDivElement)}
style={{ overflow: "auto", width: "100%", height: "100%" }}
>
@@ -252,6 +262,7 @@ export default function Table({
emptyState ?? <EmptyState sx={{ py: 8 }} />
) : (
<TableBody
containerEl={containerEl}
containerRef={containerRef}
leafColumns={leafColumns}
rows={rows}

View File

@@ -24,6 +24,12 @@ import {
} from "./Table";
export interface ITableBodyProps {
/**
* Re-render this component when the container element changes, to fix a bug
* where virtualization doesnt detect scrolls if `containerRef.current` was
* initially null
*/
containerEl: HTMLDivElement | null;
/** Used in `useVirtualization` */
containerRef: React.RefObject<HTMLDivElement>;
/** Used in `useVirtualization` */