Skip to content

Commit

Permalink
fix(useVisibilityChange): useEffect return null & destroy is not a fu…
Browse files Browse the repository at this point in the history
…nction form UT with jest (#730)
  • Loading branch information
JardelCheung committed Jul 16, 2024
1 parent 0de3dd6 commit 119ca2c
Showing 1 changed file with 22 additions and 23 deletions.
45 changes: 22 additions & 23 deletions packages/react-vant/src/components/hooks/use-visibility-change.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,33 @@ export default function useVisibilityChange(
const [state, setState] = useState<boolean>()
useEffect(() => {
// compatibility: https://caniuse.com/#feat=intersectionobserver
if (!inBrowser || !window.IntersectionObserver) {
return null
}
const observer = new IntersectionObserver(
entries => {
// visibility changed
onChange?.(entries[0].intersectionRatio > 0)
for (const entry of entries) {
setState(entry.isIntersecting)
}
},
{ root: document.body }
)
if (!(!inBrowser || !window.IntersectionObserver)) {
const observer = new IntersectionObserver(
entries => {
// visibility changed
onChange?.(entries[0].intersectionRatio > 0)
for (const entry of entries) {
setState(entry.isIntersecting)
}
},
{ root: document.body }
)

const observe = () => {
if (target.current) {
observer.observe(target.current)
const observe = () => {
if (target.current) {
observer.observe(target.current)
}
}
}

const unobserve = () => {
if (target.current) {
observer.unobserve(target.current)
const unobserve = () => {
if (target.current) {
observer.unobserve(target.current)
}
}
}

observe()
return unobserve
observe()
return unobserve
}
}, [target.current])

return [state] as const
Expand Down

0 comments on commit 119ca2c

Please sign in to comment.