Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | import { useState, useRef } from 'react';
export default function HookRef() {
const id = useRef(null);
const [count, setCount] = useState(0);
const handleStart = () => {
if (id.current === null) {
id.current = setInterval(() => setCount(c => c + 1), 1000);
}
};
const handleEnd = () => {
clearInterval(id.current);
id.current = null;
};
return (
<>
<button onClick={handleStart}>시작</button>
<button onClick={handleEnd}>종료</button>
<p>{count}초 경과</p>
</>
);
} |