Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 5193 | Ir a la última revisión | | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
5188 stevensc 1
import { useEffect, useState } from 'react'
2
 
3
const useNear = (ref, rootMargin = '0px') => {
4
  // State and setter for storing whether element is visible
5
  const [isIntersecting, setIntersecting] = useState(false)
6
 
7
  const reset = () => setIntersecting(false)
8
 
9
  useEffect(() => {
10
    const observer = new IntersectionObserver(
11
      ([entry]) => {
12
        // Update our state when observer callback fires
13
        setIntersecting(entry.isIntersecting)
14
      },
15
      {
16
        rootMargin
17
      }
18
    )
19
    if (ref.current) {
20
      observer.observe(ref.current)
21
    }
22
    return () => {
23
      observer.unobserve(ref.current)
24
    }
25
  }, []) // Empty array ensures that effect is only run on mount and unmount
26
 
27
  return [isIntersecting, reset]
28
}
29
 
30
export default useNear