You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Track when element appears without causing re-renders
115
+
consttrackingRef=useOnInView((entry) => {
116
+
// Element is in view - perhaps log an impression
117
+
console.log("Element appeared in view", entry.target);
118
+
119
+
// Return optional cleanup function
120
+
return () => {
121
+
console.log("Element left view");
122
+
};
123
+
}, {
124
+
/* Optional options */
125
+
threshold:0.5,
126
+
trigger:"enter",
127
+
triggerOnce:true,
128
+
});
129
+
130
+
return (
131
+
<div ref={trackingRef}>
132
+
<h2>This element is being tracked without re-renders</h2>
133
+
</div>
134
+
);
135
+
};
136
+
```
137
+
74
138
### Render props
75
139
76
140
To use the `<InView>` component, you pass it a function. It will be called
@@ -145,6 +209,13 @@ Provide these as the options argument in the `useInView` hook or as props on the
145
209
| **initialInView** | `boolean` | `false` | Set the initial value of the `inView` boolean. This can be used if you expect the element to be in the viewport to start with, and you want to trigger something when it leaves. |
146
210
| **fallbackInView** | `boolean` | `undefined` | If the `IntersectionObserver` API isn't available in the client, the default behavior is to throw an Error. You can set a specific fallback behavior, and the `inView` value will be set to this instead of failing. To set a global default, you can set it with the `defaultFallbackInView()` |
147
211
212
+
`useOnInView` accepts the same options as `useInView` except `onChange`,
213
+
`initialInView`, and `fallbackInView`, and adds the following configuration:
| **trigger** | `"enter"` or `"leave"` | `"enter"` | Decide whether the callback runs when the element enters (`"enter"`) or leaves (`"leave"`) the viewport. |
218
+
148
219
### InView Props
149
220
150
221
The **`<InView />`** component also accepts the following props:
0 commit comments