MCPcopy Index your code
hub / github.com/bitmap/react-hook-inview / useInView

Function useInView

src/useInView.ts:48–94  ·  view source on GitHub ↗
(
  { root, rootMargin, threshold, unobserveOnEnter, target, onEnter, onLeave } = {},
  externalState = [],
)

Source from the content-addressed store, hash-verified

46 * @param externalState React.ComponentState[]
47 */
48const useInView: UseInView = (
49 { root, rootMargin, threshold, unobserveOnEnter, target, onEnter, onLeave } = {},
50 externalState = [],
51) => {
52 const [state, setState] = useState<State>({
53 inView: false,
54 entry: null,
55 observer: null,
56 });
57
58 const callback = useCallback<IntersectionObserverCallback>(([entry], observer) => {
59 const inThreshold = observer.thresholds.some((t) => entry.intersectionRatio >= t);
60 const inView = inThreshold && entry.isIntersecting;
61
62 setState({
63 inView,
64 entry,
65 observer,
66 });
67
68 // unobserveOnEnter
69 if (inView && unobserveOnEnter) {
70 observer.unobserve(entry.target);
71 observer.disconnect();
72 }
73
74 // Legacy callbacks
75 if (inView) {
76 onEnter?.(entry, observer);
77 } else {
78 onLeave?.(entry, observer);
79 }
80 }, [onEnter, onLeave, unobserveOnEnter]);
81
82 const setTarget = useObserver(
83 callback,
84 { root, rootMargin, threshold },
85 [unobserveOnEnter, ...externalState],
86 );
87
88 // Legacy 'target' option
89 useEffect(() => {
90 if (target?.current) setTarget(target.current);
91 }, [target, setTarget]);
92
93 return [setTarget, state.inView, state.entry, state.observer];
94};
95
96export default useInView;

Callers 4

useInView.test.tsxFile · 0.85
ComponentFunction · 0.85
ComponentWithRootFunction · 0.85
InViewFunction · 0.85

Calls 1

useObserverFunction · 0.85

Tested by 2

ComponentFunction · 0.68
ComponentWithRootFunction · 0.68