MCPcopy
hub / github.com/thebuilder/react-intersection-observer / InView

Class InView

src/InView.tsx:65–210  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

63 ```
64 */
65export class InView extends React.Component<
66 IntersectionObserverProps | PlainChildrenProps,
67 State
68> {
69 node: Element | null = null;
70 _unobserveCb: (() => void) | null = null;
71 lastInView: boolean | undefined;
72
73 constructor(props: IntersectionObserverProps | PlainChildrenProps) {
74 super(props);
75 this.state = {
76 inView: !!props.initialInView,
77 entry: undefined,
78 };
79 this.lastInView = props.initialInView;
80 }
81
82 componentDidMount() {
83 this.unobserve();
84 this.observeNode();
85 }
86
87 componentDidUpdate(prevProps: IntersectionObserverProps) {
88 // If a IntersectionObserver option changed, reinit the observer
89 if (
90 prevProps.rootMargin !== this.props.rootMargin ||
91 prevProps.root !== this.props.root ||
92 prevProps.threshold !== this.props.threshold ||
93 prevProps.skip !== this.props.skip ||
94 prevProps.trackVisibility !== this.props.trackVisibility ||
95 prevProps.delay !== this.props.delay
96 ) {
97 this.unobserve();
98 this.observeNode();
99 }
100 }
101
102 componentWillUnmount() {
103 this.unobserve();
104 }
105
106 observeNode() {
107 if (!this.node || this.props.skip) return;
108 const {
109 threshold,
110 root,
111 rootMargin,
112 trackVisibility,
113 delay,
114 fallbackInView,
115 } = this.props;
116
117 if (this.lastInView === undefined) {
118 this.lastInView = this.props.initialInView;
119 }
120 this._unobserveCb = observe(
121 this.node,
122 this.handleChange,

Callers

nothing calls this directly

Calls 3

unobserveMethod · 0.95
observeNodeMethod · 0.95
isPlainChildrenFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…