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

Mobile: countdown does not repeat when using useState function update in onComplete #51

Closed
vydimitrov opened this issue Sep 5, 2020 · 3 comments
Assignees
Labels
bug Something isn't working mobile React Native

Comments

@vydimitrov
Copy link
Owner

vydimitrov commented Sep 5, 2020

This bug affects only React Native countdown timer. The Web/React version on the countdown timer works as expected.

It was found that when setting a new state (using useState hook for example) in the onComplete handler, the component was going into infinite loop. The component uses internally useEeffect to track changes of play state, which also has depenedency of onComplete. When the component re-renders while in finished state (waiting the onCompelte to return) the onCompletehandler was called again causing the infinite loop.

Resolution: to solve the bug, I am now tracking the finished state. While the component is in finished state, a.k.a waiting for the onComplete handler to return, if new updates come they will be ignored until the onComplete finishes. Here is the commit that fixes the issue 0ea726d

@vydimitrov vydimitrov self-assigned this Sep 5, 2020
@vydimitrov vydimitrov added bug Something isn't working mobile React Native labels Sep 5, 2020
@JRyanSaunders
Copy link

JRyanSaunders commented Sep 6, 2020

Hi @vydimitrov I have been messaging you on twitter under the name Kappiey. I have been trying to implement this in my project for a couple of days now but I can't get it to work. I was wondering if you could take a look?

https://github.com/JRyanSaunders/Fitness-App

@vydimitrov
Copy link
Owner Author

vydimitrov commented Sep 6, 2020

Hey @JRyanSaunders, your app helped me a lot to find the root cause of the bug, thanks for sharing it. The issue was caused by a loop in the onComplete callback. Since we were setting state in the callback, the parent component was re-rendered thus a new instance of the onComplete callback was passed, which caused to run the callback in a loop.

To solve it, I am now checking if we are in finished state and it that is the case I don't call the callback again. Here is the commit that fixes the issue

Please upgrade the package to 2.3.9 and you should have it fixed now.

Thanks for your time 👍

@JRyanSaunders
Copy link

I'm glad I could help @vydimitrov - thanks for making such a useful component! I have reverted back to my original code and everything is working perfectly now!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working mobile React Native
Projects
None yet
Development

No branches or pull requests

2 participants