mirror of
https://github.com/qier222/YesPlayMusic.git
synced 2024-12-03 14:03:35 +08:00
26 lines
565 B
TypeScript
26 lines
565 B
TypeScript
import { useState, useEffect, RefObject } from 'react'
|
|
|
|
const useIntersectionObserver = (
|
|
element: RefObject<Element>
|
|
): { onScreen: boolean } => {
|
|
const [onScreen, setOnScreen] = useState(false)
|
|
|
|
useEffect(() => {
|
|
if (element.current) {
|
|
const observer = new IntersectionObserver(([entry]) =>
|
|
setOnScreen(entry.isIntersecting)
|
|
)
|
|
observer.observe(element.current)
|
|
return () => {
|
|
observer.disconnect()
|
|
}
|
|
}
|
|
}, [element, setOnScreen])
|
|
|
|
return {
|
|
onScreen,
|
|
}
|
|
}
|
|
|
|
export default useIntersectionObserver
|