Skip to content

feat(scrollOffset): return scrollOffset from useVirtual#266

Closed
dantman wants to merge 1 commit into
TanStack:mainfrom
dantman:return-scrollOffset
Closed

feat(scrollOffset): return scrollOffset from useVirtual#266
dantman wants to merge 1 commit into
TanStack:mainfrom
dantman:return-scrollOffset

Conversation

@dantman

@dantman dantman commented Feb 19, 2022

Copy link
Copy Markdown

What is the change?

useVirtual now returns its scrollOffset state.

Docs, tests, and types for the behaviour have been included.

Motivation

There are a variety of reasons you may want to implement behaviour that is dependent on the current scroll offset of the virtualizer. e.g. Pagination UI like "displaying rows 1-5" or a "Scroll to top" that only shows when scrollOffset > tooCloseToStart.

Right now to implement this kind of behaviour the host component needs to add its own onScroll handler and update a state. However useVirtual already has a scrollOffset state updated once on every scroll event resulting in a re-render. As a result if the host component adds its own onScroll that changes state, then the host component will re-render twice on every single scroll event. Even though useVirtual already has all the information that is needed to implement that kind of behaviour and the 2nd re-render is entirely unnecessary and would halve the component's performance. Additionally the UI could experience tearing as the render where useVirtual's new list of items to display and the render where the pagination UI is updated are separate renders.

Here's a demo showing the double render behaviour which would be avoided if useVirtual didn't hide the scrollOffset.
https://codesandbox.io/s/usevirtual-with-an-extra-scroll-event-3q6jqz?file=/src/main.jsx

@vercel

vercel Bot commented Feb 19, 2022

Copy link
Copy Markdown

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/tanstack/react-virtual/CF7L8GdHRZQw6Y5MQX6qfT5TWhcn
✅ Preview: https://react-virtual-git-fork-dantman-return-scrolloffset-tanstack.vercel.app

@duttaoindril

duttaoindril commented May 27, 2022

Copy link
Copy Markdown

@tannerlinsley Can we get this merged? It'd be really helpful for me! :)

@tannerlinsley

Copy link
Copy Markdown
Member

This PR is outdated and should either be closed, re-architected or rebased against v3 beta. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants