react-compound-timer icon indicating copy to clipboard operation
react-compound-timer copied to clipboard

Controlling one instance of the timer from another instance

Open udayms opened this issue 5 years ago • 3 comments

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?

udayms avatar Mar 14 '19 17:03 udayms

@volkov97 Can you pls give me any pointers on the above?

udayms avatar Apr 02 '19 18:04 udayms

I believe you can use refs to call the timer control functions

an0nEmouse avatar May 09 '19 03:05 an0nEmouse

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()
}

lyuben-todorov avatar Jan 15 '20 00:01 lyuben-todorov