react-compound-timer
react-compound-timer copied to clipboard
Controlling one instance of the timer from another instance
Hi... Thanks for this really awesome component. I am a newbie with very minimal background in react programming. I have a screen where I have 2 timers. And checkpoints in one timer resets and triggers the other. For ex: Timer 1 starts and 30 seconds and goes backward. And Timer 2 starts with initialTime set to 5 seconds when the first checkpoint is hit. And then when the second checkpoint is hit, Timer 2 resets again with a initialTime time set to 10 seconds. So, I want to be able to set an initialTime and then start the Timer 2 from within the checkpoint functions of Timer 1. Is this very difficult to do?
@volkov97 Can you pls give me any pointers on the above?
I believe you can use refs to call the timer control functions
Put this in your constructor
constructor(props) {
super(props);
this.userTimer = React.createRef();
}
then pass the ref to Timer props:
<Timer ref={this.userTimer} ... />
You can now access Timer control functions from the component lifecycle methods. For example:
componentDidMount() {
this.userTimer.current.stop()
}