Dashboard: Fix lazy loading for repeated panels (#43083)

This commit is contained in:
kay delaney 2021-12-14 11:23:42 +00:00 committed by GitHub
parent 61227998a7
commit c926d4d7f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -15,6 +15,7 @@ export function LazyLoader({ children, width, height, onLoad, onChange }: Props)
const [loaded, setLoaded] = useState(false);
const [isInView, setIsInView] = useState(false);
const wrapperRef = useRef<HTMLDivElement>(null);
useEffectOnce(() => {
LazyLoader.addCallback(id, (entry) => {
if (!loaded && entry.isIntersecting) {
@ -26,12 +27,15 @@ export function LazyLoader({ children, width, height, onLoad, onChange }: Props)
onChange?.(entry.isIntersecting);
});
if (wrapperRef.current) {
LazyLoader.observer.observe(wrapperRef.current);
const wrapperEl = wrapperRef.current;
if (wrapperEl) {
LazyLoader.observer.observe(wrapperEl);
}
return () => {
delete LazyLoader.callbacks[id];
wrapperEl && LazyLoader.observer.unobserve(wrapperEl);
if (Object.keys(LazyLoader.callbacks).length === 0) {
LazyLoader.observer.disconnect();
}