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
Copy file name to clipboardExpand all lines: README.md
+32-11Lines changed: 32 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -30,7 +30,7 @@ or NPM:
30
30
npm install react-intersection-observer --save
31
31
```
32
32
33
-
> ⚠️You also want to add the [intersection-observer](https://www.npmjs.com/package/react-intersection-observer) polyfill for full browser support. Check out adding the [polyfill](#polyfill) for details about how you can include it.
33
+
> ⚠️ You also want to add the [intersection-observer](https://www.npmjs.com/package/react-intersection-observer) polyfill for full browser support. Check out adding the [polyfill](#polyfill) for details about how you can include it.
34
34
35
35
## Usage
36
36
@@ -40,7 +40,7 @@ To use the `Observer`, you pass it a function. It will be called whenever the st
40
40
In addition to the `inView` prop, children also receives a `ref` that should be set on the containing DOM element.
41
41
This is the element that the IntersectionObserver will monitor.
42
42
43
-
```js
43
+
```jsx
44
44
importObserverfrom'react-intersection-observer'
45
45
46
46
constComponent= () => (
@@ -56,19 +56,40 @@ const Component = () => (
56
56
exportdefaultComponent
57
57
```
58
58
59
+
### Plain children
60
+
61
+
You can pass any element to the `<Observer />`, and it will handle creating the wrapping DOM element.
62
+
Add a handler to the `onChange` method, and control the state in your own component.
63
+
It will pass any extra props to the HTML element, allowing you set the `className`, `style`, etc.
|**children**| ({inView, ref}) => Node || true | Children expects a function that recieves an object contain an `inView` boolean and `ref` that should be assigned to the element root. |
66
-
|**onChange**| (inView) => void || false | Call this function whenever the in view state changes |
67
-
|**root**| HTMLElement || false | The HTMLElement that is used as the viewport for checking visibility of the target. Defaults to the browser viewport if not specified or if null. |
68
-
|**rootId**| String || false | Unique identifier for the root element - This is used to identify the IntersectionObserver instance, so it can be reused. If you defined a root element, without adding an id, it will create a new instance for all components. |
69
-
|**rootMargin**| String | '0px' | false | Margin around the root. Can have values similar to the CSS margin property, e.g. "10px 20px 30px 40px" (top, right, bottom, left). |
70
-
|**threshold**| Number | 0 | false | Number between 0 and 1 indicating the the percentage that should be visible before triggering. Can also be an array of numbers, to create multiple trigger points. |
71
-
|**triggerOnce**| Bool | false | false | Only trigger this method once |
|**children**| ({inView, ref}) => React.Node / React.Node || true | Children expects a function that receives an object contain an `inView` boolean and `ref` that should be assigned to the element root. Alternately pass a plain child, to have the `<Observer />` deal with the wrapping element.|
87
+
|**onChange**| (inView) => void || false | Call this function whenever the in view state changes|
88
+
|**root**| HTMLElement || false | The HTMLElement that is used as the viewport for checking visibility of the target. Defaults to the browser viewport if not specified or if null.|
89
+
|**rootId**| String || false | Unique identifier for the root element - This is used to identify the IntersectionObserver instance, so it can be reused. If you defined a root element, without adding an id, it will create a new instance for all components.|
90
+
|**rootMargin**| String | '0px' | false | Margin around the root. Can have values similar to the CSS margin property, e.g. "10px 20px 30px 40px" (top, right, bottom, left).|
91
+
|**threshold**| Number | 0 | false | Number between 0 and 1 indicating the the percentage that should be visible before triggering. Can also be an array of numbers, to create multiple trigger points.|
92
+
|**triggerOnce**| Bool | false | false | Only trigger this method once|
/** Children expects a function that recieves an object contain an `inView` boolean and `ref` that should be assigned to the element root. */
8
-
children?: ({
9
-
inView: boolean,
10
-
ref: (node: ?HTMLElement)=>void,
11
-
})=>React.Node,
7
+
/** Children expects a function that receives an object contain an `inView` boolean and `ref` that should be assigned to the element root. */
8
+
children?:
9
+
|(({
10
+
inView: boolean,
11
+
ref: (node: ?HTMLElement)=>void,
12
+
})=>React.Node)
13
+
|React.Node,
12
14
/** @deprecated replace render with children */
13
15
render?: ({
14
16
inView: boolean,
15
17
ref: (node: ?HTMLElement)=>void,
16
18
})=>React.Node,
17
-
/** @deprecated */
19
+
/** Element tag to use for the wrapping element when rendering a plain React.Node. Defaults to 'div' */
18
20
tag?: string,
19
21
/** Number between 0 and 1 indicating the the percentage that should be visible before triggering. Can also be an array of numbers, to create multiple trigger points. */
20
22
threshold?: number|Array<number>,
@@ -61,11 +63,6 @@ class Observer extends React.Component<Props, State> {
61
63
`react-intersection-observer: "render" is deprecated, and should be replaced with "children"`,
62
64
this.node,
63
65
)
64
-
}elseif(typeofthis.props.children!=='function'){
65
-
console.warn(
66
-
`react-intersection-observer: plain "children" is deprecated. You should convert it to a function that handles the "ref" manually.`,
0 commit comments