Skip to content

Commit

Permalink
slight performance tweak on defaultSize
Browse files Browse the repository at this point in the history
  • Loading branch information
mckervinc committed Oct 4, 2023
1 parent 783f7d7 commit 6921195
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
// don't force es6 functions to include space before paren
"space-before-function-paren": 0,
"prettier/prettier": "error",
"no-debugger": true,
"no-debugger": 2,
"semi": "off"
}
}
2 changes: 1 addition & 1 deletion src/Row.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ function Row<T>({
const onExpanderClick = useCallback(() => {
dispatch({ type: "updateExpanded", key: generateKeyFromRow(row, index) });
expandedCalledRef.current = true;
}, [dispatch, row, index, generateKeyFromRow, expandedCalledRef]);
}, [dispatch, row, index, generateKeyFromRow]);

const resetHeight = useCallback(() => {
if (!rowRef.current || !pixelWidths.length) {
Expand Down
14 changes: 6 additions & 8 deletions src/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ const ListComponent = forwardRef(function (
const treeRef = useRef(new NumberTree());
const tableRef = useRef<HTMLDivElement>(null);
const cacheRef = useRef<{ [index: number]: number }>({});
const defaultSizeRef = useRef(rowHeight || DEFAULT_ROW_HEIGHT);
const { dispatch, uuid, columns, minColumnWidth, fixedWidth, remainingCols, pixelWidths } =
useContext(TableContext);
const [useRowWidth, setUseRowWidth] = useState(true);
const [defaultSize, setDefaultSize] = useState(rowHeight || DEFAULT_ROW_HEIGHT);

// constants
const hasFooter = useMemo(() => {
Expand Down Expand Up @@ -107,17 +107,18 @@ const ListComponent = forwardRef(function (
const row = typeof queryParam === "number" ? findRowByUuidAndKey(uuid, key) : queryParam;

if (!row) {
return cacheRef.current[dataIndex] || defaultSize;
return cacheRef.current[dataIndex] || defaultSizeRef.current;
}

const arr = [...row.children].slice(rowHeight ? 1 : 0) as HTMLElement[];
const res = arr.reduce((pv, c) => pv + c.offsetHeight, rowHeight || 0) || defaultSize;
const res =
arr.reduce((pv, c) => pv + c.offsetHeight, rowHeight || 0) || defaultSizeRef.current;

// update the calculated height ref
cacheRef.current[dataIndex] = res;
return res;
},
[uuid, data, rowHeight, defaultSize, generateKeyFromRow]
[uuid, data, rowHeight, generateKeyFromRow]
);

const updatePixelWidths = useCallback(() => {
Expand Down Expand Up @@ -275,10 +276,7 @@ const ListComponent = forwardRef(function (
}
});

const median = treeRef.current.getMedian();
if (median && defaultSize !== median) {
setDefaultSize(median);
}
defaultSizeRef.current = treeRef.current.getMedian();
}}
itemData={{
rows: data,
Expand Down

0 comments on commit 6921195

Please sign in to comment.