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

useAnimateGroup doesn't work as expected after the second time the play function gets called #97

Closed
ali-garajian opened this issue Jun 11, 2022 · 1 comment · Fixed by #99

Comments

@ali-garajian
Copy link
Contributor

ali-garajian commented Jun 11, 2022

Describe the bug
useAnimateGroup's play function only works for the first two times it's called. After that the value of isPlaying doesn't get updated correctly and remains true no matter what is passed to play

To Reproduce
You can test this behavior in the demo provided in the docs for this hook : https://react-simple-animate.vercel.app/hooks

Expected behavior
Calling the play function should update the isPlaying state correctly

Screenshots
image

Additional context
the cause of this issue is how the play function is internally defined.

playRef.current = playRef.current
    ? playRef.current
    : (isPlay: boolean) => {
        ...
        setStyles(isPlay ? styles : [...styles].reverse());
        setPlaying(!isPlaying);
      };

As you can see, the function assigned to playRef.current is defined only on the first render and creates a closure on the value of isPlaying, which is initially false. Then, when we call play(!isPlaying), isPlaying gets set to true. But after the first render, because playRef.current keeps its old reference which has closed on the initial value of isPlaying, being false, calling play would always call setPlaying(true).

The solution would be to use the parameter isPlay and replace setPlaying(!isPlaying) with setPlaying(isPlay)

Also there is no test for this use case.

If you agree with the diagnosis, I'd be happy to create a PR

@bluebill1049
Copy link
Member

Send us a PR, that would be lovely.

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