Skip to content

Commit 12a9065

Browse files
committed
use the same onChange signature
1 parent 10db13d commit 12a9065

File tree

5 files changed

+87
-348
lines changed

5 files changed

+87
-348
lines changed

README.md

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -90,42 +90,41 @@ const inViewRef = useOnInView(
9090
9191
The `useOnInView` hook provides a more direct alternative to `useInView`. It
9292
takes a callback function and returns a ref that you can assign to the DOM
93-
element you want to monitor. When the element enters the viewport, your callback
94-
will be triggered.
93+
element you want to monitor. Whenever the element enters or leaves the viewport,
94+
your callback will be triggered with the latest in-view state.
9595
9696
Key differences from `useInView`:
9797
- **No re-renders** - This hook doesn't update any state, making it ideal for
9898
performance-critical scenarios
9999
- **Direct element access** - Your callback receives the actual
100100
IntersectionObserverEntry with the `target` element
101-
- **Optional cleanup** - Return a function from your callback to run when the
102-
element leaves the viewport
101+
- **Boolean-first callback** - The callback receives the current `inView`
102+
boolean as the first argument, matching the `onChange` signature from
103+
`useInView`
103104
- **Similar options** - Accepts all the same [options](#options) as `useInView`
104105
except `onChange`, `initialInView`, and `fallbackInView`
105106
106-
The `trigger` option allows to listen for the element entering the viewport or
107-
leaving the viewport. The default is `enter`.
108-
109107
```jsx
110108
import React from "react";
111109
import { useOnInView } from "react-intersection-observer";
112110

113111
const Component = () => {
114112
// Track when element appears without causing re-renders
115-
const trackingRef = 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+
const trackingRef = 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+
);
129128

130129
return (
131130
<div ref={trackingRef}>
@@ -210,11 +209,7 @@ Provide these as the options argument in the `useInView` hook or as props on the
210209
| **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()` |
211210
212211
`useOnInView` accepts the same options as `useInView` except `onChange`,
213-
`initialInView`, and `fallbackInView`, and adds the following configuration:
214-
215-
| Name | Type | Default | Description |
216-
| ----------- | --------------------- | --------- | -------------------------------------------------------------------------------------------------------------------------------------------- |
217-
| **trigger** | `"enter"` or `"leave"` | `"enter"` | Decide whether the callback runs when the element enters (`"enter"`) or leaves (`"leave"`) the viewport. |
212+
`initialInView`, and `fallbackInView`.
218213
219214
### InView Props
220215

0 commit comments

Comments
 (0)