mirror of
https://github.com/qier222/YesPlayMusic.git
synced 2024-12-11 11:43:39 +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
|