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
* refactor: rewrite the component
This removes `render`, and changes `children` to only accept a function
* refactor: accept plain children with deprecation warning
* test: test the deprecated methods
* test: reset NODE_ENV
* fix: spread props to the plain child
* style: apply prettier
* refactor: log the DOM node in deprecations
|**children**|Func/Node || false |Children should be either a function or a node |
99
-
|**render**| ({inView, ref}) => Node || false| Render prop allowing you to control the view. |
64
+
|**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.|
65
+
|**onChange**| (inView) => void || false | Call this function whenever the in view state changes|
100
66
|**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. |
101
67
|**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. |
102
68
|**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). |
103
-
|**tag**| String | 'div' | false | Element tag to use for the wrapping element when rendering using 'children'. Defaults to 'div' |
104
69
|**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. |
105
70
|**triggerOnce**| Bool | false | false | Only trigger this method once |
106
-
|**onChange**| Func || false | Call this function whenever the in view state changes |
/** Render prop boolean indicating inView state */
7
+
/** 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,
12
+
/** @deprecated replace render with children */
14
13
render?: ({
15
14
inView: boolean,
16
15
ref: (node: ?HTMLElement)=>void,
17
16
})=>React.Node,
17
+
/** @deprecated */
18
+
tag?: string,
18
19
/** 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. */
19
20
threshold?: number|Array<number>,
21
+
/** Only trigger the inView callback once */
22
+
triggerOnce: boolean,
20
23
/** 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.*/
21
24
root?: HTMLElement,
22
25
/** Margin around the root. Can have values similar to the CSS margin property, e.g. "10px 20px 30px 40px" (top, right, bottom, left). */
@@ -36,14 +39,13 @@ type State = {
36
39
* Monitors scroll, and triggers the children function with updated props
37
40
*
38
41
<Observer>
39
-
{inView => (
40
-
<h1>{`${inView}`}</h1>
42
+
{({inView, ref}) => (
43
+
<h1 ref={ref}>{`${inView}`}</h1>
41
44
)}
42
45
</Observer>
43
46
*/
44
47
classObserverextendsReact.Component<Props,State>{
45
48
static defaultProps={
46
-
tag: 'div',
47
49
threshold: 0,
48
50
triggerOnce: false,
49
51
}
@@ -53,10 +55,21 @@ class Observer extends React.Component<Props, State> {
53
55
}
54
56
55
57
componentDidMount(){
56
-
if(typeofthis.props.render==='function'){
58
+
if(process.env.NODE_ENV!=='production'){
59
+
if(this.props.hasOwnProperty('render')){
60
+
console.warn(
61
+
`react-intersection-observer: "render" is deprecated, and should be replaced with "children"`,
62
+
this.node,
63
+
)
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.`,
67
+
this.node,
68
+
)
69
+
}
57
70
invariant(
58
71
this.node,
59
-
`react-intersection-observer: No DOM node found. Make sure you forward "ref" to the root DOM element you want to observe, when using render prop.`,
72
+
`react-intersection-observer: No DOM node found. Make sure you forward "ref" to the root DOM element you want to observe.`,
60
73
)
61
74
}
62
75
}
@@ -131,20 +144,16 @@ class Observer extends React.Component<Props, State> {
0 commit comments