question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Scrolling to item on load

See original GitHub issue

I 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:open
  • Created 3 years ago
  • Comments:7

github_iconTop GitHub Comments

31reactions
jancama2commented, Sep 24, 2020

This won’t work because of two things:

  1. a dependency of the useEffect is a ref, which never changes, or a ref.current value, which changes, but a component won’t re-render itself because of ref.current change (only state or props do it)
  2. useEffect is in a bad scope
function CalendarList({ width, height }) {
  const listRef = useRef()

  useEffect(() => {
    listRef.current.scrollToItem(10)
  }, [])

  return (
        <VariableSizeList
          itemCount={100}
          itemSize={200}
          height={height}
          width={width}
          ref={listRef}
        >
          {({ index, style }) => (
            <Month index={index} key={index} style={style} />
          )}
        </VariableSizeList>
  )
}

function Calendar() {
  return (
    <AutoSizer>
      {({ width, height }) => (
        <CalendarList width={width} height={height} />
      )}
    </AutoSizer>
  )
}

This ensures useEffect is called only when the VariableSizeList is actually mounted. The reason why you need to do this is because of AutoSizer which “takes some time” to render its children.

3reactions
jancama2commented, Sep 29, 2020

@pavsidhu np, btw would be better to use useLayoutEffect to avoid some ui flickering

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found