@@ -32,16 +32,12 @@ const ScrollRenderer = (props: Props) => {
32
32
}
33
33
} ;
34
34
35
- const onScroll = ( ) => {
36
- const span : any = containerRef . current ;
37
-
38
- if ( span ) {
39
- const startingPoint = span . parentNode . offsetTop + span . parentNode . offsetHeight ;
40
- const anchorPos = span . offsetTop - span . parentNode . scrollTop ;
35
+ const onScroll = ( span : any ) => ( ) => {
36
+ const startingPoint = span . parentNode . offsetTop + span . parentNode . offsetHeight ;
37
+ const anchorPos = span . offsetTop - span . parentNode . scrollTop ;
41
38
42
- if ( anchorPos <= ( startingPoint + ( span . parentNode . offsetHeight * 2 ) ) ) {
43
- requestAnimationFrame ( ( ) => addItem ( span . parentNode , span . parentNode . scrollTop ) ) ;
44
- }
39
+ if ( anchorPos <= ( startingPoint + ( span . parentNode . offsetHeight * 2 ) ) ) {
40
+ requestAnimationFrame ( ( ) => addItem ( span . parentNode , span . parentNode . scrollTop ) ) ;
45
41
}
46
42
} ;
47
43
@@ -68,25 +64,26 @@ const ScrollRenderer = (props: Props) => {
68
64
useLayoutEffect ( ( ) => {
69
65
const span : any = containerRef . current ;
70
66
let container : any = null ;
67
+ const handleScroll = onScroll ( span ) ;
71
68
if ( span ) {
72
69
container = span . parentNode ;
73
70
// populate double the container height of items
74
- if ( span . parentNode . scrollHeight <= ( container . offsetHeight * 2 ) ) {
71
+ if ( render . index === 0 || container . scrollHeight <= ( container . offsetHeight * 2 ) ) {
75
72
requestAnimationFrame ( ( ) => addItem ( container ) ) ;
76
73
}
77
74
78
75
if ( render . index > 0 && dataList . length === render . renderList . length ) {
79
- container . removeEventListener ( 'scroll' , onScroll , true ) ;
76
+ container . removeEventListener ( 'scroll' , handleScroll , true ) ;
80
77
} else {
81
- container . addEventListener ( 'scroll' , onScroll , true ) ;
78
+ container . addEventListener ( 'scroll' , handleScroll , true ) ;
82
79
}
83
80
84
81
adding = false ;
85
82
}
86
83
87
84
return ( ) => { // when unmounted
88
85
if ( span ) {
89
- container . removeEventListener ( 'scroll' , onScroll , true ) ;
86
+ container . removeEventListener ( 'scroll' , handleScroll , true ) ;
90
87
}
91
88
} ;
92
89
} , [ render . index ] ) ;
0 commit comments