@@ -15,21 +15,15 @@ type Props = Language & {
15
15
16
16
const LazyLoadComponent : FC < Component & Language & {
17
17
path ?: string
18
- } > = ( { name, lang, path, loadComponent } ) => {
18
+ observer : ( cb : ( ) => void ) => IntersectionObserver ;
19
+ } > = ( { name, lang, path, loadComponent, observer } ) => {
19
20
const history = useHistory ( ) ;
20
21
const ref = useRef < HTMLDivElement > ( null ) ;
21
22
const [ isVisible , setIsVisible ] = useState < boolean > ( false ) ;
22
23
23
24
useEffect ( ( ) => {
24
- const observer = new IntersectionObserver ( ( entries ) => {
25
- entries . forEach ( ( entry ) => {
26
- if ( entry . isIntersecting ) {
27
- setIsVisible ( true ) ;
28
- }
29
- } )
30
- } ) ;
31
- observer . observe ( ref . current as Element ) ;
32
- } , [ ] ) ;
25
+ observer ( ( ) => setIsVisible ( true ) ) . observe ( ref . current as Element ) ;
26
+ } , [ observer ] ) ;
33
27
34
28
const formattedComponentName = name . charAt ( 0 ) . toUpperCase ( ) + name . slice ( 1 ) ;
35
29
@@ -73,12 +67,19 @@ export const ComponentList: FC<Props> = ({ lang }) => {
73
67
if ( components ?. length <= 0 ) return null
74
68
const componentLength = components . length
75
69
const headline = lang === "de" ? `Anzahl Komponenten: ${ componentLength } ` : `Components sum: ${ componentLength } `
70
+ const observer = useCallback ( ( cb : ( ) => void ) => new IntersectionObserver ( ( entries ) => {
71
+ entries . forEach ( ( entry ) => {
72
+ if ( entry . isIntersecting ) {
73
+ cb ( )
74
+ }
75
+ } )
76
+ } ) , [ ] ) ;
76
77
return (
77
78
< >
78
79
< KolHeading _label = { headline } _level = { 3 } > { headline } </ KolHeading >
79
80
< div className = "components-overview" >
80
81
{ components . map ( ( { name, loadComponent } ) => (
81
- < LazyLoadComponent key = { name } name = { name } lang = { lang } path = { preferredVersion ?. path } loadComponent = { loadComponent } />
82
+ < LazyLoadComponent key = { name } name = { name } lang = { lang } path = { preferredVersion ?. path } loadComponent = { loadComponent } observer = { observer } />
82
83
) ) }
83
84
</ div >
84
85
</ >
0 commit comments