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

How to call start/pause from another function/component?

Open tigersze opened this issue 5 years ago • 3 comments

It is possible to call the stop/start function from other functions? I cannot find any example. Can anyone please help me? Thanks.

tigersze avatar Jun 01 '20 20:06 tigersze

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>

joaonzangoII avatar Jun 27 '20 13:06 joaonzangoII

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>

douglasrcjames avatar Dec 15 '20 06:12 douglasrcjames

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.

peterzanetti avatar Nov 24 '21 20:11 peterzanetti