Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strange Scrollyteller v2.0 behaviour disregarding useState changes #18

Closed
phocks opened this issue Nov 26, 2019 · 5 comments · Fixed by #26
Closed

Strange Scrollyteller v2.0 behaviour disregarding useState changes #18

phocks opened this issue Nov 26, 2019 · 5 comments · Fixed by #26

Comments

@phocks
Copy link
Member

phocks commented Nov 26, 2019

When you use a Scrollyteller component and pass an onMarker function... and then try to use a useState state variable eg [year, setYear] = useState(1999) the onMarker function will not respond to changes to year when we call setYear and will always show the original value "1999".

Seems to be something to do with the way function components work..... had to revert to version 1 of scrollyteller

@katiaeirin
Copy link
Contributor

I ran into the same issue using scrollyteller inside a functional component .

I think this is related to facebook/react#14066 and not passing dependencies inside useEffect array in Scrollyteller. If I pass onScroll or this.props.onMarker inside the array, I can get the updated state inside my onMarker callback, but that introduces all sorts of other issues because useEffect runs more than once and scroll listeners are attached every time.

Did you work out a way to fix it?

I think one way would be to update Scrollyteller component to be a class and use componentDidMount instead.

For now I changed my parent component to class and it seems to be working fine.

@phocks
Copy link
Member Author

phocks commented Jul 15, 2020

Yeah it used to be a class component in version 1 before Nathan upgraded it to be a function component. Maybe it was the wrong way to go.... unless we can find a fix.

@drzax
Copy link
Member

drzax commented Jul 15, 2020

I'm not at all familiar with this issue, but I'd say that's almost certainly the problem @katiaeirin (if it's not, it's bound to be causing other problems).

The onScroll function should really be defined inside the useEffect hook. Could we do that, then wrap this.props.onMarker in a useRef hook and pass the resulting reference in the dependency array on useEffect?

@phocks
Copy link
Member Author

phocks commented Jul 15, 2020

Seems like it could work. I'd have to try reproducing the problem to test, unless @katiaeirin you have some time to work on this a bit?

@katiaeirin
Copy link
Contributor

Yep I can try doing that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants