react-compound-timer
react-compound-timer copied to clipboard
How to call start/pause from another function/component?
It is possible to call the stop/start function from other functions? I cannot find any example. Can anyone please help me? Thanks.
You can get the reference of the Component then create your functions to access the Component functions
<Timer ref={(node) => { this.timerRef = node; }} ... > {({ start, resume, pause, stop, reset, timerState }) => ( ... )} </Timer>
For clarity on usage outside the <Timer> block:
<Timer ref={(node) => { this.timerRef = node; }}>
{({ start, resume, pause, stop, reset, timerState }) => (
<>
<h3>Timer: </h3><Timer.Seconds />
</>
)}
</Timer>
<button onClick={() => {this.timerRef.stop()}}>stop timer</button>
<button onClick={() => {this.timerRef.start()}}>start timer</button>
Ick. Refs. No chance of just passing props into the component itself for things like start/resume/pause/stop/reset? That would have been infinitely more useful.