VisibilitySensor onChange triggers on render
See original GitHub issueMy 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:
- Created 6 years ago
- Reactions:7
- Comments:7 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
This might be the same issue I’m having in #102. I’ve opened #103 that fixes this.