Skip to content

Commit

Permalink
fix(table): avoid unnecessary re-rendering of table rows
Browse files Browse the repository at this point in the history
  • Loading branch information
rivka-ungar committed Dec 4, 2024
1 parent bd22a25 commit 716738d
Showing 1 changed file with 15 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { ComponentType, forwardRef, useCallback, useEffect } from "react";
import React, { ComponentType, forwardRef, useCallback, useEffect, useMemo } from "react";
import { VibeComponentProps } from "../../../types";
import TableBody from "../TableBody/TableBody";
import styles from "./TableVirtualizedBody.module.scss";
Expand Down Expand Up @@ -101,6 +101,19 @@ const TableVirtualizedBody = forwardRef(
if (!virtualizedWithHeader) markTableAsVirtualized();
}, [markTableAsVirtualized, virtualizedWithHeader]);

const memoizedInnerElementType = useMemo(
() =>
virtualizedWithHeader
? forwardRef(({ children, ...rest }: any, ref: React.Ref<HTMLDivElement>) => (
<div ref={ref} {...rest}>
{headerRenderer!(columns!)}
{children}
</div>
))
: undefined,
[virtualizedWithHeader, headerRenderer, columns]
);

return (
<TableBody
className={cx(
Expand All @@ -126,16 +139,7 @@ const TableVirtualizedBody = forwardRef(
outerRef={element => {
virtualizedListRef.current = element;
}}
innerElementType={
virtualizedWithHeader
? forwardRef(({ children, ...rest }, ref) => (
<div ref={ref} {...rest}>
{headerRenderer(columns)}
{children}
</div>
))
: undefined
}
innerElementType={memoizedInnerElementType}
>
{itemRenderer}
</List>
Expand Down

0 comments on commit 716738d

Please sign in to comment.