React In Viewport Component
Wrapper component to detect if the component is in viewport. Use Intersection Observer API
Usages
Wrap your component with handleViewport HOC, you will receive inViewport
props indicating the component is in viewport or not.
handleViewport
HOC accepts three params
- Component
- Options: second param is the option you want to pass to Intersection Observer API.
- Config object:
disconnectOnLeave { Boolean }
disconnect intersection observer after leave
The HOC preserve onEnterViewport
and onLeaveViewport
props as a callback
NOTE: Stateless: Need to add ref={this.props.innerRef}
on your component
- If you need to know how many times the component has entered the viewport use the prop
enterCount
. - If you need to know how many times the component has left the viewport use the prop
leaveCount
.