Skip to content

Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.

613 B
Raw Blame
import React, { useEffect, useState } from 'react'
import { useInView } from 'react-intersection-observer'

export default function LazyLoader(props) {
  const [hasBecomeVisibleYet, setHasBecomeVisibleYet] = useState(false)
  const { ref, inView } = useInView({
    threshold: 0
  })

  useEffect(() => {
    if (inView && !hasBecomeVisibleYet) {
      setHasBecomeVisibleYet(true)
      if (props.onVisible) props.onVisible()
    }
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [inView])

  return (
    <div ref={ref} className="flex-1 flex flex-col">
      {props.children}
    </div>
  )
}