Skip to content

Commit

Permalink
refactor: memoizing a computed value for list size
Browse files Browse the repository at this point in the history
  • Loading branch information
JulienIzz committed Feb 8, 2024
1 parent 8513cd7 commit 918c2fd
Showing 1 changed file with 9 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
import { NodeOrientation } from '../../types/orientation';
import { typedMemo } from '../../helpers/TypedMemo';
import { getLastLeftItemIndex, getLastRightItemIndex } from './helpers/getLastItemIndex';
import { getSizeInPxFromOneItemToAnother } from './helpers/getSizeInPxFromOneItemToAnother';

/**
* @TODO: VirtualizedList should be able to take any data as params.
Expand Down Expand Up @@ -159,6 +160,11 @@ export const VirtualizedList = typedMemo(

const listSizeInPx = vertical ? height : width;

const totalVirtualizedListSize = useMemo(
() => getSizeInPxFromOneItemToAnother(data, itemSize, 0, data.length),
[data, itemSize],
);

const dataSliceToRender = data.slice(range.start, range.end + 1);

const maxPossibleLeftAlignedIndex = getLastLeftItemIndex<T>(data, itemSize, listSizeInPx);
Expand Down Expand Up @@ -236,25 +242,14 @@ export const VirtualizedList = typedMemo(
* RowWidth = Screen Width + size of the item on left
* ```
*/

const getTotalVirtualizedListSize = useCallback(
(data: T[], itemSize: number | ((item: T) => number)) => {
if (typeof itemSize === 'number') {
return data.reduce((acc) => acc + itemSize, 0);
}
return data.reduce((acc, item) => acc + itemSize(item), 0);
},
[],
);

const dimensionStyle = useMemo(
() =>
vertical
? ({
height: getTotalVirtualizedListSize(data, itemSize),
height: totalVirtualizedListSize,
} as const)
: ({ width: getTotalVirtualizedListSize(data, itemSize) } as const),
[data, getTotalVirtualizedListSize, itemSize, vertical],
: ({ width: totalVirtualizedListSize } as const),
[totalVirtualizedListSize, vertical],
);

return (
Expand Down

0 comments on commit 918c2fd

Please sign in to comment.