Skip to content
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

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/Clock.js
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());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 부분은 매초 Date객체를 생성하는데, 기존의 time에서 1초를 더해주는 코드로 대체해도 괜찮을 것 같아요~!

}, 1000);
return () => clearInterval(id);
}, []);
Comment on lines +10 to +14

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

언마운트 될 때 clear 해준 거 좋습니다 ㅎㅎ


const timeOptions = {
Copy link
Member

Choose a reason for hiding this comment

The 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",
};

Choose a reason for hiding this comment

The 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>
);
};
Expand Down