Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
27 changes: 2 additions & 25 deletions src/components/CountdownExample.jsx
Original file line number Diff line number Diff line change
@@ -1,31 +1,8 @@
import { useState, useEffect } from "react";
import { useCountdown } from "../hooks/useCountdown";

export const CountdownExample = () => {
const targetDate = new Date("2025-08-25T00:00:00");

const calculateTimeLeft = (targetDate) => {
const difference = targetDate.getTime() - new Date().getTime();

if (difference <= 0) {
return { days: 0, hours: 0, minutes: 0, seconds: 0 };
}

const days = Math.floor(difference / (1000 * 60 * 60 * 24));
const hours = Math.floor((difference / (1000 * 60 * 60)) % 24);
const minutes = Math.floor((difference / (1000 * 60)) % 60);
const seconds = Math.floor((difference / 1000) % 60);

return { days, hours, minutes, seconds };
};
const [timeLeft, setTimeLeft] = useState(() => calculateTimeLeft(targetDate));

useEffect(() => {
const timer = setInterval(() => {
setTimeLeft(calculateTimeLeft(targetDate));
}, 1000);

return () => clearInterval(timer);
}, [targetDate]);
const timeLeft = useCountdown(targetDate);

return (
<div>
Expand Down
23 changes: 2 additions & 21 deletions src/components/WindowSizeExample.jsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,8 @@
import { useEffect, useState } from "react";
import { useWindowSize } from "../hooks/useWindowSize";

export const WindowSizeExample = () => {
// 실습 1. 하단 코드를 useWindowSize (커스텀 훅으로 바꿔주세요!)
const [windowSize, setWindowSize] = useState({
width: window.innerWidth,
height: window.innerHeight,
});

useEffect(() => {
const handleResize = () => {
setWindowSize({
width: window.innerWidth,
height: window.innerHeight,
});
};

window.addEventListener("resize", handleResize);

return () => {
window.removeEventListener("resize", handleResize);
};
}, []);

const { windowSize } = useWindowSize();
return (
<div>
<h2>useWindowSize 실습</h2>
Expand Down
11 changes: 8 additions & 3 deletions src/components/YourOwnHookPage.jsx

Choose a reason for hiding this comment

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

간단하지만 아이디어가 좋은 것 같아요
필요 없는 주석은 삭제해도 좋을 것 같아요

Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { useSomething } from "../hooks/useSomething";
import { useTextColor } from "../hooks/useTextColor";

export const YourOwnHookPage = () => {
// const { something... } = useSomething();
// 하단 UI에 자유롭게 위에서 받아온 값들을 바인딩 해보세요~
const { color, changeColor } = useTextColor();

return (
<div>
<h2>useSomething 실습</h2>
<h2 style={{ color }}>useSomething 실습</h2>
<button className="modern-btn" onClick={changeColor}>
색상 변경
</button>
</div>
);
};
};
26 changes: 26 additions & 0 deletions src/hooks/useCountdown.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,29 @@
import { useState, useEffect } from "react";
const calculateTimeLeft = (targetDate) => {
const difference = targetDate.getTime() - new Date().getTime();

if (difference <= 0) {
return { days: 0, hours: 0, minutes: 0, seconds: 0 };
}

const days = Math.floor(difference / (1000 * 60 * 60 * 24));
const hours = Math.floor((difference / (1000 * 60 * 60)) % 24);
const minutes = Math.floor((difference / (1000 * 60)) % 60);
const seconds = Math.floor((difference / 1000) % 60);

return { days, hours, minutes, seconds };
};

export const useCountdown = (targetDate) => {
const [timeLeft, setTimeLeft] = useState(() => calculateTimeLeft(targetDate));

useEffect(() => {
const timer = setInterval(() => {
setTimeLeft(calculateTimeLeft(targetDate));
}, 1000);

return () => clearInterval(timer);
}, [targetDate]);

return timeLeft;
};
5 changes: 0 additions & 5 deletions src/hooks/useSomething.js

This file was deleted.

17 changes: 17 additions & 0 deletions src/hooks/useTextColor.js

Choose a reason for hiding this comment

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

색상 배열의 개수에 맞게 Math.floor(Math.random() * colors.length) 라고 코드 잘 작성해주신 것 같아요!

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { useState } from 'react';

export const useTextColor = () => {
// 여러분의 use{Something}을 만들어주세요!

Choose a reason for hiding this comment

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

커스텀 훅의 목적이 분명해서 보기 좋아요!
글자 색상을 바꾸는 로직이라는 게 useTextColor라는 이름만 봐도 바로 느껴져서, 함수명을 직관적으로 잘 지으신 것 같아용

// 정답은 없습니다. 커스텀훅의 필요성을 스스로 느껴보세요.
// 아이디어를 생각하고, 스스로 구현하다가 어려우면 손 들어주세요!
const [color, setColor] = useState('black');

const changeColor = () => {
// 예시로 색상을 랜덤하게 바꾸거나, 정해진 목록에서 바꿀 수 있음
const colors = ['red', 'blue', 'green', 'orange', 'purple'];

Choose a reason for hiding this comment

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

정해진 배열에서 색깔이 랜덤으로 뽑히게 한 점이 인상깊어요!

const nextColor = colors[Math.floor(Math.random() * colors.length)];

Choose a reason for hiding this comment

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

목록에서의 선택과 랜덤 변경을 모두 가능하게 해 둔 점이 인상 깊어요! UI에서 랜덤 선택 버튼을 따로 만들어봐도 재밌을 것 같습니다

Choose a reason for hiding this comment

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

버튼 클릭할 때마다 텍스트 색이 달라지는 경험이 직관적이면서도 재밌는 것 같습니다!

Comment on lines +7 to +12

Choose a reason for hiding this comment

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

changeColor 시 기존 색상과 같은 값이 다시 나올 수 있는데 while 루프나 필터링으로 개선 가능할 것 같습니다

setColor(nextColor);
};
Copy link
Member

Choose a reason for hiding this comment

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

정해진 목록뿐만이 아니라 랜덤으로 색상 변경하는 것까지 구현하셨네요!

Comment on lines +10 to +14
Copy link

Choose a reason for hiding this comment

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

현재 색상과 다른 색만 나오도록 필터링해보는 것도 괜찮을 것 같아요!

Copy link

Choose a reason for hiding this comment

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

랜덤으로 색을 고르도록 잘 구현하신 것 같아요.!!


return { color, changeColor };
};
25 changes: 25 additions & 0 deletions src/hooks/useWindowSize.js
Original file line number Diff line number Diff line change
@@ -1 +1,26 @@
// 커스텀훅 코드를 작성해보세요!
import { useEffect, useState } from "react";

export const useWindowSize = () => {
const [windowSize, setWindowSize] = useState({
width: window.innerWidth,
height: window.innerHeight,
});

useEffect(() => {
const handleResize = () => {
setWindowSize({
width: window.innerWidth,
height: window.innerHeight,
});
};

window.addEventListener("resize", handleResize);

return () => {
window.removeEventListener("resize", handleResize);
};
}, []);

return { windowSize };
};