1
- import React , { useRef , useEffect } from 'react'
1
+ import { useRef , useEffect } from 'react'
2
2
import type { ReactNode } from 'react'
3
3
import { NavLink } from 'react-router-dom'
4
4
import classNames from 'classnames'
@@ -22,35 +22,39 @@ const AppList = ({
22
22
className
23
23
} : AppListProps ) => {
24
24
const lazyImageRefs = useRef < Array < HTMLImageElement | null > > ( [ ] )
25
- const lazyImages = useRef < Array < HTMLImageElement | null > > ( [ ] )
26
-
27
25
const initLazyImgLoad = ( ) => {
28
- lazyImages . current = lazyImageRefs . current . map ( ( el ) => el )
29
- $_xsl__loadImgLazy ( lazyImages . current )
26
+ $_xsl__loadImgLazy ( lazyImageRefs . current as any )
30
27
}
31
28
useEffect ( ( ) => {
32
- lazyImageRefs . current = lazyImageRefs . current . slice ( 0 , data . length )
33
29
initLazyImgLoad ( )
34
30
} , [ data , render , className ] )
35
31
36
32
useEffect ( ( ) => {
37
33
initLazyImgLoad ( )
38
34
} , [ ] )
39
35
36
+ const setLazyImageElement = (
37
+ index : number ,
38
+ element : HTMLImageElement | null
39
+ ) => {
40
+ lazyImageRefs . current [ index ] = element
41
+ return ( ) => {
42
+ lazyImageRefs . current . splice ( index , 1 )
43
+ }
44
+ }
45
+
40
46
return (
41
47
< ul
42
48
className = { classNames ( styles . AppList , className ) }
43
- onScroll = { ( ) => $_xsl__loadImgLazy ( lazyImages . current ) }
49
+ onScroll = { ( ) => $_xsl__loadImgLazy ( lazyImageRefs . current as any ) }
44
50
>
45
51
{ data . map ( ( item , index ) => {
46
52
return (
47
53
< li className = { classNames ( styles . AppList__item , 'main_border_bottom' ) } key = { item . path } >
48
54
< NavLink to = { item . path } className = { styles . AppList__link } >
49
55
< img
50
56
className = { classNames ( styles . AppList__img , 'lazyImage' ) }
51
- ref = { ( el ) => {
52
- lazyImageRefs . current [ index ] = el
53
- } }
57
+ ref = { ( el ) => setLazyImageElement ( index , el ) }
54
58
src = { logo__grey }
55
59
data-src = { item . imgurl }
56
60
alt = { item . name }
0 commit comments