Skip to content

Commit 119ca2c

Browse files
authored
fix(useVisibilityChange): useEffect return null & destroy is not a function form UT with jest (#730)
1 parent 0de3dd6 commit 119ca2c

File tree

1 file changed

+22
-23
lines changed

1 file changed

+22
-23
lines changed

packages/react-vant/src/components/hooks/use-visibility-change.ts

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,33 @@ export default function useVisibilityChange(
99
const [state, setState] = useState<boolean>()
1010
useEffect(() => {
1111
// compatibility: https://caniuse.com/#feat=intersectionobserver
12-
if (!inBrowser || !window.IntersectionObserver) {
13-
return null
14-
}
15-
const observer = new IntersectionObserver(
16-
entries => {
17-
// visibility changed
18-
onChange?.(entries[0].intersectionRatio > 0)
19-
for (const entry of entries) {
20-
setState(entry.isIntersecting)
21-
}
22-
},
23-
{ root: document.body }
24-
)
12+
if (!(!inBrowser || !window.IntersectionObserver)) {
13+
const observer = new IntersectionObserver(
14+
entries => {
15+
// visibility changed
16+
onChange?.(entries[0].intersectionRatio > 0)
17+
for (const entry of entries) {
18+
setState(entry.isIntersecting)
19+
}
20+
},
21+
{ root: document.body }
22+
)
2523

26-
const observe = () => {
27-
if (target.current) {
28-
observer.observe(target.current)
24+
const observe = () => {
25+
if (target.current) {
26+
observer.observe(target.current)
27+
}
2928
}
30-
}
3129

32-
const unobserve = () => {
33-
if (target.current) {
34-
observer.unobserve(target.current)
30+
const unobserve = () => {
31+
if (target.current) {
32+
observer.unobserve(target.current)
33+
}
3534
}
36-
}
3735

38-
observe()
39-
return unobserve
36+
observe()
37+
return unobserve
38+
}
4039
}, [target.current])
4140

4241
return [state] as const

0 commit comments

Comments
 (0)