Skip to content

Commit

Permalink
fix(lazyloader): fix trying to run inexistent callbacks (#833)
Browse files Browse the repository at this point in the history
  • Loading branch information
svennergr committed Jul 17, 2024
1 parent e02d89a commit c8fac96
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions packages/scenes/src/components/layout/LazyLoader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ export const LazyLoader: LazyLoaderType = React.forwardRef<HTMLDivElement, Props
}

return () => {
delete LazyLoader.callbacks[id];
wrapperEl && LazyLoader.observer.unobserve(wrapperEl);
delete LazyLoader.callbacks[id];
if (Object.keys(LazyLoader.callbacks).length === 0) {
LazyLoader.observer.disconnect();
}
Expand All @@ -62,8 +62,8 @@ export const LazyLoader: LazyLoaderType = React.forwardRef<HTMLDivElement, Props

// If the element was loaded, we add the `hideEmpty` class to potentially
// hide the LazyLoader if it does not have any children. This is the case
// when children have the `isHidden` property set.
// We always include the `className` class, as this is coming from the
// when children have the `isHidden` property set.
// We always include the `className` class, as this is coming from the
// caller of the `LazyLoader` component.
const classes = `${loaded ? hideEmpty : ''} ${className}`;
return (
Expand All @@ -90,7 +90,9 @@ LazyLoader.addCallback = (id: string, c: (e: IntersectionObserverEntry) => void)
LazyLoader.observer = new IntersectionObserver(
(entries) => {
for (const entry of entries) {
LazyLoader.callbacks[entry.target.id](entry);
if (typeof LazyLoader.callbacks[entry.target.id] === 'function') {
LazyLoader.callbacks[entry.target.id](entry);
}
}
},
{ rootMargin: '100px' }
Expand Down

0 comments on commit c8fac96

Please sign in to comment.