11"use strict" ;
22
3- import React from "react" ;
3+ import React , { useState , useCallback , useRef } from "react" ;
44import ReactDOM from "react-dom" ;
5- import VisibilitySensor from "../visibility-sensor" ;
6-
7- class Example extends React . Component {
8- constructor ( props ) {
9- super ( props ) ;
10-
11- this . state = {
12- msg : ""
13- } ;
14- }
15-
16- onChange = isVisible => {
17- this . setState ( {
18- msg : "Element is now " + ( isVisible ? "visible" : "hidden" )
19- } ) ;
20- } ;
21-
22- render ( ) {
23- return (
24- < div >
25- < p className = "msg" > { this . state . msg } </ p >
26- < div className = "before" />
27- < VisibilitySensor
28- scrollCheck
29- scrollThrottle = { 100 }
30- intervalDelay = { 8000 }
31- containment = { this . props . containment }
32- onChange = { this . onChange }
33- minTopValue = { this . props . minTopValue }
34- partialVisibility = { this . props . partialVisibility }
35- offset = { this . props . offset }
36- >
37- { ( { getRef } ) => < div ref = { getRef ( ) } className = "sensor" /> }
38- </ VisibilitySensor >
39- < div className = "after" />
40- </ div >
41- ) ;
42- }
5+ import VisibilitySensor , { useVisibilitySensor } from "../visibility-sensor" ;
6+
7+ function RegularExample ( { containment, minTopValue, partialVisibility } ) {
8+ const [ msg , setMsg ] = useState ( "" ) ;
9+ const onChange = useCallback ( isVisible => {
10+ setMsg ( "Element is now " + ( isVisible ? "visible" : "hidden" ) ) ;
11+ } , [ ] ) ;
12+
13+ return (
14+ < div >
15+ < p className = "msg" > { msg } </ p >
16+ < div className = "before" />
17+ < VisibilitySensor
18+ scrollCheck = { true }
19+ scrollThrottle = { 100 }
20+ intervalDelay = { 8000 }
21+ onChange = { onChange }
22+ >
23+ { ( ) => < div className = "sensor" /> }
24+ </ VisibilitySensor >
25+ < div className = "after" />
26+ </ div >
27+ ) ;
28+ }
29+
30+ function HookExample ( { containment, minTopValue, partialVisibility } ) {
31+ const sensorRef = useRef ( ) ;
32+ const [ msg , setMsg ] = useState ( "" ) ;
33+ const onChange = useCallback ( isVisible => {
34+ setMsg ( "Element is now " + ( isVisible ? "visible" : "hidden" ) ) ;
35+ } , [ ] ) ;
36+
37+ useVisibilitySensor ( sensorRef , {
38+ scrollCheck : true ,
39+ scrollThrottle : 100 ,
40+ intervalDelay : 8000 ,
41+ containment,
42+ minTopValue : 10 ,
43+ partialVisibility : true ,
44+ onChange
45+ } ) ;
46+
47+ return (
48+ < div >
49+ < p className = "msg" > { msg } </ p >
50+ < div className = "before" />
51+ < div ref = { sensorRef } className = "sensor" />
52+ < div className = "after" />
53+ </ div >
54+ ) ;
4355}
4456
4557ReactDOM . render (
46- React . createElement ( Example ) ,
58+ React . createElement ( RegularExample ) ,
4759 document . getElementById ( "example" )
4860) ;
4961
@@ -54,10 +66,8 @@ container.scrollTop = 320;
5466container . scrollLeft = 320 ;
5567
5668ReactDOM . render (
57- React . createElement ( Example , {
58- containment : container ,
59- minTopValue : 10 ,
60- partialVisibility : true
69+ React . createElement ( HookExample , {
70+ containment : container
6171 } ) ,
6272 elem
6373) ;
0 commit comments