joshwnj / react-visibility-sensor

Sensor component for React that notifies you when it goes in or out of the window viewport.
MIT License
2.33k stars 195 forks source link

Can I get a callback if an element is in view for more than x seconds? #150

Open ArbaazDossani opened 5 years ago

ArbaazDossani commented 5 years ago

I need a callback if the user has stopped scrolling and an element is in view for more than x seconds

ghost1face commented 5 years ago

This can probably be done by separating your components and using lifecycle hooks (ComponentDidMount & ComponentWillUnmount), for example:

class MyComponent extends React.PureComponent {
   componentDidMount() {
     this._timeout = setTimeout(() => {
         // make request after being mounted for 5 seconds
     }, 5000);
  }

  componentWillUnmount() {
     clearTimeout(this._timeout);
  }
}

<VisibilitySensor>
    {({isVisible}) => <MyComponent />
</VisibilitySensor>