fkhadra / react-on-screen

Check if a react component in the viewport
https://fkhadra.github.io/react-on-screen/
MIT License
405 stars 50 forks source link

Allowing to set the initial state of the component #28

Open dgopsq opened 6 years ago

dgopsq commented 6 years ago

Hello, I'm using this module to track an element at the top of the page (and so visible by default). In the first load of the page this component always returns false and then the correct true, while if I reload the page when the component is not visible by default (i.e. if I reload while i'm in the middle of the page) I get only false which is correct.

You can see a replica here, and here's the code.

Edit: Okay, I checked the code and I see that the first false makes sense, since it's the default state, and I don't get a second value when I refresh while not seeing the element because the state remain false. This is quite a tricky thing, because I have an animation that triggers when isVisible === false, and so happens to trigger in the first load because of the default state. I tried to ignore that state but if a refresh happens in the middle of the page then the animation won't trigger.

I think allowing to set the initial state of the component could be an interesting solution, couldn't it?