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
-
});
113
+
consttrackingRef=useOnInView(
114
+
(inView, entry) => {
115
+
if (inView) {
116
+
// Element is in view - perhaps log an impression
117
+
console.log("Element appeared in view", entry.target);
118
+
} else {
119
+
console.log("Element left view", entry.target);
120
+
}
121
+
},
122
+
{
123
+
/* Optional options */
124
+
threshold:0.5,
125
+
triggerOnce:true,
126
+
},
127
+
);
129
128
130
129
return (
131
130
<div ref={trackingRef}>
@@ -210,11 +209,7 @@ Provide these as the options argument in the `useInView` hook or as props on the
210
209
| **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()` |
211
210
212
211
`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. |
0 commit comments