feat(scrollOffset): return scrollOffset from useVirtual#266
Closed
dantman wants to merge 1 commit into
Closed
Conversation
|
This pull request is being automatically deployed with Vercel (learn more). 🔍 Inspect: https://vercel.com/tanstack/react-virtual/CF7L8GdHRZQw6Y5MQX6qfT5TWhcn |
|
@tannerlinsley Can we get this merged? It'd be really helpful for me! :) |
Member
|
This PR is outdated and should either be closed, re-architected or rebased against v3 beta. Thanks! |
3 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
onScrollhandler and update a state. HoweveruseVirtualalready has ascrollOffsetstate updated once on every scroll event resulting in a re-render. As a result if the host component adds its ownonScrollthat changes state, then the host component will re-render twice on every single scroll event. Even thoughuseVirtualalready 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