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
22 changes: 2 additions & 20 deletions src/components/WindowSizeExample.jsx
Original file line number Diff line number Diff line change
@@ -1,26 +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>
Expand Down
10 changes: 7 additions & 3 deletions src/components/YourOwnHookPage.jsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { useSomething } from "../hooks/useSomething";
import { useName } from "../hooks/useMyName";

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

return (
<div>
<h2>useSomething 실습</h2>
<p>{name}</p>
<button className="modern-btn" onClick={changeName}>
누구게

Choose a reason for hiding this comment

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

효과가 많아서 재미있는 것 같아요
이름이 뜬 후에는 버튼 이름을 누구게 말고 다른 걸로 바꾸면 좋을 것 같아요

</button>
</div>
);
};
13 changes: 12 additions & 1 deletion src/hooks/useConfetti.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,16 @@ export const useConfetti = () => {
origin: { y: 0.6 },
});
};
return { fire };

const fire2 = () => {
confetti({
particleCount: 100,
spread: 360,
origin: {
x: Math.random(),
y: Math.random() - 0.2,

Choose a reason for hiding this comment

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

Math.random()을 넣어서 confetti가 화면 곳곳에서 터지는 효과가 다채롭고 재밌는 것 같아요!

},
});
};
return { fire, fire2 };
};
12 changes: 12 additions & 0 deletions src/hooks/useCountdown.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
import { useEffect, useState } from "react";
import { calculateTimeLeft } from "../utils/calculateTime";

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

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

return () => clearInterval(timer);
}, [targetDate]);
return timeLeft;
};
23 changes: 23 additions & 0 deletions src/hooks/useMyName.js

Choose a reason for hiding this comment

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

단순히 name만 바뀌는 게 아니라 fire2()라는 효과도 같이 줘서 좋은 것 같아요!

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { useEffect, useState } from "react";
import { useConfetti } from "./useConfetti";

export const useName = () => {
// 여러분의 use{name}을 만들어주세요!
// 정답은 없습니다. 커스텀훅의 필요성을 스스로 느껴보세요.
// 아이디어를 생각하고, 스스로 구현하다가 어려우면 손 들어주세요!
const [name, setName] = useState("아기사자");
const { fire2 } = useConfetti();
Comment on lines +8 to +9

Choose a reason for hiding this comment

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

이름 상태를 관리하면서 변경될 때마다 useConfetti의 fire2 효과를 실행하는 좋은 코드인 것 같습니다


const changeName = () => {
setName((prev) => (prev === "아기사자" ? "아기사자 백지연" : "아기사자"));

Choose a reason for hiding this comment

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

상태 업데이트를 prev 기반으로 처리해서 최신 상태를 안전하게 반영시킨 점이 인상 깊습니다!

Choose a reason for hiding this comment

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

prev를 활용해 최신 상태 기준으로 안전하게 업데이트하는 점 배워갑니다아

};

useEffect(() => {
Copy link

Choose a reason for hiding this comment

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

name 값이 바뀔 때마다 useEffect로 confetti가 실행되게 해둔 점이 좋네용!! 그리고 name변경과 confetti 실행 코드가 분리되어있어서 깔끔합니다

fire2();
}, [name]);

return {
name,
changeName,
};
};
5 changes: 0 additions & 5 deletions src/hooks/useSomething.js

This file was deleted.

26 changes: 26 additions & 0 deletions src/hooks/useWindowSize.js
Original file line number Diff line number Diff line change
@@ -1 +1,27 @@
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 };
};
14 changes: 14 additions & 0 deletions src/utils/calculateTime.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export 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 };
};