bvaughn / react-window

React components for efficiently rendering large lists and tabular data
https://react-window.now.sh/
MIT License
15.72k stars 782 forks source link

Preserve scrollOffset before navigation. #633

Closed TaeyoonKwon closed 2 years ago

TaeyoonKwon commented 2 years ago

I am using Next + Recoil for my project and trying to preserve current scrollOffset of react-window so I can route back to where I was.

I tried both onScroll and onItemsRendered methods, but they automatically set the offset to 0 when first loaded, so it always becomes 0 when it's loaded.

Could be a problem with AutoSizer. How should I solve this?

 const [homeScrollPosition, setHomeScrollPosition] = useRecoilState(
   homeScrollPositionState
 );

<AutoSizer>
 {({ height, width }) => (
    <List ref={listRef} 
         onItemsRendered={(v) => {
                    if (listRef?.current) {
                      setHomeScrollPosition(v.visibleStartIndex);
                    }
          }}>
     ...