From ea6b541a4e3d85d515c4d51af522643ec2be65c1 Mon Sep 17 00:00:00 2001 From: Wincent <3088566+davidwincent@users.noreply.github.com> Date: Fri, 5 Mar 2021 16:42:21 +0100 Subject: [PATCH] feat: Add measure method, issue #31 (#55) * feat: Add measure method, issue #31 * refactor: measure method Reduce roundtrips * Fix: virtualItems should not depend on state * Fix: remove eslint-disable-next-line react-hooks/exhaustive-deps Co-authored-by: David Wincent Co-authored-by: David Wincent --- src/index.js | 6 ++++++ types/index.d.ts | 1 + 2 files changed, 7 insertions(+) diff --git a/src/index.js b/src/index.js index 6f5e0a5f..31f98aa5 100644 --- a/src/index.js +++ b/src/index.js @@ -47,6 +47,11 @@ export function useVirtual({ const [measuredCache, setMeasuredCache] = React.useState({}) + const measure = React.useCallback( + () => setMeasuredCache({}), + [] + ) + const measurements = React.useMemo(() => { const measurements = [] for (let i = 0; i < size; i++) { @@ -215,6 +220,7 @@ export function useVirtual({ totalSize, scrollToOffset, scrollToIndex, + measure, } } diff --git a/types/index.d.ts b/types/index.d.ts index 7490c40c..7ce67e3b 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -39,6 +39,7 @@ declare function useVirtual(options: { totalSize: number scrollToOffset: (index: number, options?: ScrollToOffsetOptions) => void scrollToIndex: (index: number, options?: ScrollToIndexOptions) => void + measure: () => void } export { useVirtual }