Scrolling to item on load
See original GitHub issueI have a situation where I want to scroll to the current month in a calendar on load. However useEffect is ran only once at the initial render when listRef.current is undefined and that’s it. I would’ve assumed useEffect to run again once listRef.current has a value however it does not. Could this be a bug? Here is the code to reproduce:
function Calendar() {
const listRef = useRef()
useEffect(() => {
listRef.current?.scrollToItem(10)
}, [listRef.current])
return (
<AutoSizer>
{({ width, height }) => (
<VariableSizeList
itemCount={100}
itemSize={200}
height={height}
width={width}
ref={listRef}
>
{({ index, style }) => (
<Month index={index} key={index} style={style} />
)}
</VariableSizeList>
)}
</AutoSizer>
)
}
Issue Analytics
- State:
- Created 3 years ago
- Comments:7
Top Results From Across the Web
Scroll down a page and load all items before using read_html()
its just a function I define to process all of the steps to open, scroll down the page etc. I call it using...
Read more >Need to scroll gallery to load itens info
Hello everyone. Items are loading according to scroll down. I want to know how can I load data in gallery without need to...
Read more >Load content on page scroll with jQuery and AJAX - Makitweb -
In this tutorial, I show how you can load content from MySQL database on page scroll using jQuery AJAX and PHP with a...
Read more >Infinite Scrolling, Pagination Or “Load More” Buttons ...
The “Load more” button threshold of 50 to 100 items determines when to interrupt the user, while the lazy-loading threshold is merely a ......
Read more >A guide to pagination, load more buttons, and infinite scroll
If you are building any list visuals on mobile that do not have infinite scroll, use the load more method. You should also...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This won’t work because of two things:
useEffect
is aref
, which never changes, or aref.current
value, which changes, but a component won’t re-render itself because ofref.current
change (only state or props do it)useEffect
is in a bad scopeThis ensures
useEffect
is called only when theVariableSizeList
is actually mounted. The reason why you need to do this is because ofAutoSizer
which “takes some time” to render its children.@pavsidhu np, btw would be better to use
useLayoutEffect
to avoid some ui flickering