
import './style.css';
import { useRef, useState, useEffect } from 'react';
function App() {
const [time, setTime] = useState(null);
const timerRef = useRef(null);
useEffect(() => {
setTime(new Date());
timerRef.current=setInterval(() => {
setTime(new Date());
}, 1000);
}, []);
function handleStop() {
clearInterval(timerRef.current);
timerRef.current = null;
}
function handleStart() {
if (timerRef.current === null) {
timerRef.current = setInterval(() => {
setTime(new Date());
}, 1000);
}
}
if (time === null) return null;
return (
<section className="app">
<div className="watch_container">
<h1>⏰ 현재시간 ⏰</h1>
<p>{time.toLocaleTimeString()}</p>
<div className="action_button_container">
<button
className="start_button_container"
onClick={handleStart}
>
시작
</button>
<button
className="stop_button_container"
onClick={handleStop}
>
멈춤
</button>
</div>
</div>
</section>
);
}
export default App;
이 코드대로 미니타이머 작업을 했는데, 멈춤을 눌러도 멈춰지지가 않네요 ㅠㅠ
어디가 잘 못 된 것인지 확인 부탁드립니다.
그리고 저번에 교재 PDF 관련에 대해 질문했는데, 아직 확인이 안 된 것인지 알려주세요 ...
