talalmajali / react-native-countdown-component

React Native CountDown
MIT License
271 stars 234 forks source link

Restart timer #12

Closed jmaricic closed 5 years ago

jmaricic commented 6 years ago

Is there a way to restart a timer once the countdown is finished or to manually start the countdown (on button press for example)?

talalmajali commented 6 years ago

@jmaricic You have to pass a new until value to the component, you can do this in onFinish callback, once the timer finished you assign a new until value to the global state that until value came from. Like if your until value came from the parent component state, in onFinish callback you reset that value, or if your until value came from redux reducer you need to dispatch an action to update that value. I think this should do the trick for you.

jmaricic commented 6 years ago

I have a global state in my component, and I've set it to 0 like this:

state = { counter: 0, };

And when I press the button I'm calling a setState:

this.setState({ counter: 59 });

And this is what the countdown looks like:

    <CountDown style={{ marginBottom: 30 }} until={this.state.counter} timeToShow={['S']} size={30} />

The problem is, my component is receiving props every 5 settings and I don't want the counter to reset on every prop received... Is there a way to achieve that?

talalmajali commented 6 years ago

@jmaricic I got your point I have pushed a fix at bfcf2a6bc21cff4b55ecc9cfc353831a5ca7f7d8 and published as 1.7.3 Thanks

aldocano29 commented 6 years ago

Hi, I tried to reset the timer, and yes... the timer gets a new until value when the countdown finishes, but it won't start counting down again.

Tatandiaz13 commented 5 years ago

hi,

i have the same problem @aldocano29 , any solution ?

Tatandiaz13 commented 5 years ago

if you still have the problem, this solution it work for me:

https://stackoverflow.com/questions/48674827/react-native-whats-the-best-way-to-remount-reset-reload-a-screen

it is a temporally solution.

talalmajali commented 5 years ago

@Tatandiaz13 @aldocano29 Will push fix soon

talalmajali commented 5 years ago

@Tatandiaz13 @aldocano29 this should be fixed with a4140c78f01ff55a0125238bab96e6e69442a76d But if you pass a same last until value after the counter has finished it wont get restarted

adybuciuman commented 5 years ago

@talalmajali But if I need to pass the same value again? I'm doing a counter for a quiz and I need to reset the counter once the answer was saved.

memanoj commented 1 year ago

Try this https://github.com/talalmajali/react-native-countdown-component/issues/92#issuecomment-1467393540