项目作者: TaskSquirrel

项目描述 :
UI components for React
高级语言: JavaScript
项目地址: git://github.com/TaskSquirrel/react-visibility.git
创建时间: 2018-07-04T18:06:06Z
项目社区:https://github.com/TaskSquirrel/react-visibility

开源协议:

下载


React Visibility

React components for tracking DOM elements in the viewport.

Install

  1. npm install react-visibility
  1. import { InView, withInView } from "react-visibility";

Usage

For all component guides, visit the docs.

Advanced: <InView>

  1. import React from "react";
  2. import { InView } from "react-visibility";
  3. const MyComponent = props => (
  4. <InView>
  5. { ({ ref }) => (
  6. <div className="some-container" ref={ref}>
  7. I am being tracked!
  8. </div>
  9. ) }
  10. </InView>
  11. );

For a less specific use case, the <InView> component provides the most control and flexibility. It accepts a function as a child component and passes an object as the single argument to props.children(). To track a specific DOM element, pass the required ref value to the ref prop of a native DOM element.

By default, the <InView> component subscribes to events in the window object, but you can pass a ref of another DOM element as the activeElement prop to listen for events there .

All components are server-side rendering compatible.

Combining import()

Combine <InView> with dynamic import() to lazy-load React components when elements are in the viewport.