-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[2주차] 김지원 미션 제출합니다. #10
base: master
Are you sure you want to change the base?
Changes from 1 commit
4ff49f9
e0ee46e
87c055f
3e73431
b7acc9c
504598f
7c9cbab
edcff18
9c361b7
80f1066
e67856e
b019e92
29997f6
1655f50
fe68124
6bafe2e
da866d4
c830c4d
e369d99
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,25 @@ | ||
import { useState, useEffect } from "react"; | ||
|
||
const Clock = () => { | ||
const [time, setTime] = useState(new Date()); | ||
|
||
useEffect(() => { | ||
const id = setInterval(() => { | ||
setTime(new Date()); | ||
}, 1000); | ||
return () => clearInterval(id); | ||
}, []); | ||
Comment on lines
+10
to
+14
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 언마운트 될 때 clear 해준 거 좋습니다 ㅎㅎ |
||
|
||
const timeOptions = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 저도 fns라이브러리를 사용했는데 이렇게 옵션으로 하는것도 좋은 방법인 것 같습니다~ |
||
hour12: false, // 24 시간 형식 사용 | ||
hour: "2-digit", | ||
minute: "2-digit", | ||
second: "2-digit", | ||
}; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 예전에 .toLocaleTimeString() 을 사용할 때 시간이 올바르게 표시되지 않아, 변환식을 자체적으로 만들어서 적용했던 기억이 있는데 timeOptions 을 이렇게 지정해줄수도 있겠군요! |
||
return ( | ||
<div className="Clock"> | ||
<h1>시계</h1> | ||
<h1>{time.toLocaleTimeString([], timeOptions)}</h1> | ||
</div> | ||
); | ||
}; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이 부분은 매초 Date객체를 생성하는데, 기존의 time에서 1초를 더해주는 코드로 대체해도 괜찮을 것 같아요~!