@@ -78,35 +78,39 @@ export const VirtualList = forwardRef(function <ITEM>(
7878 const [ forceRerender , setforceRerender ] = useState ( [ ] ) ; // change value to force rerender
7979 const ignoreUpdateScrollTopOnce = useRef ( false ) ;
8080 //
81- const totalSpace = itemSize * count
82- let topSpace = scrollTop - buffer
83- let bottomSpace = totalSpace - scrollTop - listSize - buffer
84- let startIndex = 0 , endIndex = 0
81+ const mainCache = useMemo ( ( ) => {
82+ const totalSpace = itemSize * count
83+ let topSpace = scrollTop - buffer
84+ let bottomSpace = totalSpace - scrollTop - listSize - buffer
85+ let startIndex = 0 , endIndex = 0
8586
86- if ( topSpace <= 0 ) {
87- topSpace = 0
88- startIndex = 0
89- } else {
90- startIndex = Math . floor ( topSpace / itemSize )
91- }
92- if ( bottomSpace < 0 ) {
93- bottomSpace = 0
94- }
95- if ( totalSpace <= listSize ) {
96- endIndex = count
97- } else {
98- endIndex = count - Math . floor ( bottomSpace / itemSize )
99- }
100- if ( ! props . virtual ) {
101- startIndex = 0
102- endIndex = props . items . length
103- }
104- const mainVisibleIndices = Array . from ( { length : endIndex - startIndex } , ( _ , index ) => index + startIndex ) ;
105- let visibleIndices = mainVisibleIndices . concat ( props . persistentIndices || [ ] )
106- if ( props . persistentIndices ?. length ) {
107- visibleIndices = [ ...new Set ( visibleIndices ) ] . sort ( ( a , b ) => a - b )
108- }
109- const visible = visibleIndices . map ( i => props . items [ i ] )
87+ if ( topSpace <= 0 ) {
88+ topSpace = 0
89+ startIndex = 0
90+ } else {
91+ startIndex = Math . floor ( topSpace / itemSize )
92+ }
93+ if ( bottomSpace < 0 ) {
94+ bottomSpace = 0
95+ }
96+ if ( totalSpace <= listSize ) {
97+ endIndex = count
98+ } else {
99+ endIndex = count - Math . floor ( bottomSpace / itemSize )
100+ }
101+ if ( ! props . virtual ) {
102+ startIndex = 0
103+ endIndex = count
104+ }
105+ const mainVisibleIndices = Array . from ( { length : endIndex - startIndex } , ( _ , index ) => index + startIndex ) ;
106+ let visibleIndices = mainVisibleIndices . concat ( props . persistentIndices || [ ] )
107+ if ( props . persistentIndices ?. length ) {
108+ visibleIndices = [ ...new Set ( visibleIndices ) ] . sort ( ( a , b ) => a - b )
109+ }
110+ const visible = visibleIndices . map ( i => props . items [ i ] )
111+ return { visible, visibleIndices, topSpace, bottomSpace, totalSpace }
112+ } , [ itemSize , count , scrollTop , buffer , listSize , props . virtual , props . persistentIndices ] ) ;
113+ const { visible, visibleIndices, topSpace, bottomSpace, totalSpace } = mainCache
110114
111115 //
112116 const listInnerStyle : any = { paddingTop : `${ topSpace } px` , boxSizing : 'border-box' }
0 commit comments