()
| 4 | // stopwatch: start, stop, reset |
| 5 | |
| 6 | function App() { |
| 7 | |
| 8 | const [time,setTime] = useState(0); |
| 9 | const intrervalIdRef = useRef(null); |
| 10 | |
| 11 | function handleStart(){ |
| 12 | |
| 13 | if(intrervalIdRef.current!=null){ |
| 14 | return; |
| 15 | } |
| 16 | |
| 17 | intrervalIdRef.current = setInterval(()=>{ |
| 18 | setTime(time=>time+1); |
| 19 | },1000); |
| 20 | } |
| 21 | |
| 22 | function handleStop(){ |
| 23 | clearInterval(intrervalIdRef.current); |
| 24 | intrervalIdRef.current = null; |
| 25 | } |
| 26 | |
| 27 | function handleReset(){ |
| 28 | clearInterval(intrervalIdRef.current); |
| 29 | intrervalIdRef.current = null; |
| 30 | setTime(0); |
| 31 | } |
| 32 | |
| 33 | |
| 34 | return( |
| 35 | <> |
| 36 | <h1>Stopwatch: {time}</h1> |
| 37 | <div> |
| 38 | <button onClick={handleStart}>Start</button> |
| 39 | <button onClick={handleStop}>Stop</button> |
| 40 | <button onClick={handleReset}>Reset</button> |
| 41 | </div> |
| 42 | </> |
| 43 | ) |
| 44 | |
| 45 | } |
| 46 | |
| 47 | export default App |
nothing calls this directly
no outgoing calls
no test coverage detected