question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

VisibilitySensor onChange triggers on render

See original GitHub issue

My component looks something like this

class MyComponent extends Component {
   onChange(isVisible){
       this.setState({visCount : this.state.visCount + 1}) // 
   }
   render(){
       return <div><VisibilitySensor onChange={(v)=>this.onChange(v)} /></div>
   }
}

The issue is onChange is getting triggered on every render, which then triggers another render so visCount keep auto-incrementing. Am I doing something wrong here? How are you supposed to use setState in onChange?

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:7
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
JadSayeghcommented, Jun 6, 2017

Hey @joshwnj , I’m trying to use it to trigger an animation on a component (animated using javascript, with setInterval) once that component becomes visible.

I haven’t looked at the code since I posted this (gave up on the animation in the interest of deadlines), but I will take a look at it again tonight and elaborate the issue.

0reactions
williambomancommented, Nov 7, 2017

This still seems to be affecting me on v3.10.1. onChange is getting called when component scrolls out of viewport, which triggers an action/reducer, state gets mapped to a prop, component gets rerendered… and onChange gets called with true again. After this onChange no longer gets triggered when scrolling the component in/out of the viewport.

This might be the same issue I’m having in #102. I’ve opened #103 that fixes this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I set VisibilitySensor to trigger the event only once?
You can trigger the active state on your onChange event by setting it to false (disable the sensor).
Read more >
How To Detect When a Component Enters the Viewport with ...
Learn how to use the React Visibility Sensor library to determine when React components have entered the viewport.
Read more >
react-visibility-sensor-mod - npm
Start using react-visibility-sensor-mod in your project by running ... When active !== true the sensor will not fire the onChange callback.
Read more >
How can I set VisibilitySensor to trigger the event only once?
const [ active, setActive ] = useState(true); const [state, setState] = useState(true); const onChange = () => setActive(false) <CountUp end={1000} ...
Read more >
Tracking pageviews in single page apps (SPA) - PostHog
This includes triggering events on page renders, using the router, and watching for ... in a VisibilitySensor and have the change trigger a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found