diff --git a/assets/js/dashboard/components/lazy-loader.js b/assets/js/dashboard/components/lazy-loader.js index c06de0de9..33adc4ff5 100644 --- a/assets/js/dashboard/components/lazy-loader.js +++ b/assets/js/dashboard/components/lazy-loader.js @@ -1,40 +1,22 @@ -import React from 'react'; +import React, { useEffect, useState } from 'react' +import { useInView } from 'react-intersection-observer' -export default class extends React.Component { - componentDidMount() { - if ('IntersectionObserver' in window) { - this.attachObserver() - } else { - this.props.onVisible && this.props.onVisible() - } - } - - attachObserver() { - this.observer = new IntersectionObserver((entries) => { - if (entries[0].isIntersecting) { - this.props.onVisible && this.props.onVisible() - this.observer.unobserve(this.element); - } - }, { - threshold: 0 - }); +export default function LazyLoader(props) { + const [hasBecomeVisibleYet, setHasBecomeVisibleYet] = useState(false) + const { ref, inView } = useInView({ + threshold: 0, + }) - this.observer.observe(this.element); - } - - componentWillUnmount() { - this.observer && this.observer.unobserve(this.element); - } + useEffect(() => { + if (inView && !hasBecomeVisibleYet) { + setHasBecomeVisibleYet(true) + props.onVisible && props.onVisible() + } + }, [inView]) - render() { - return ( -