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
25 changes: 2 additions & 23 deletions src/components/CountdownExample.jsx
Original file line number Diff line number Diff line change
@@ -1,31 +1,10 @@
import { useState, useEffect } from "react";
import {useCountdown} from "../hooks/useCountdown"

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

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]);

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
9 changes: 5 additions & 4 deletions src/components/YourOwnHookPage.jsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { useSomething } from "../hooks/useSomething";
import { useErasebtn } from "../hooks/useErasebtn";

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

return (
<div>
<h2>useSomething 실습</h2>
{erase ? <div>안녕</div> : null}

Choose a reason for hiding this comment

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

erase ? … : null 로 간결하게 표현한 부분이 인상깊어요!

<button onClick={toggleErase}>눌러보기</button>
Copy link
Member

Choose a reason for hiding this comment

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

깔끔해서 딱히 할말이 없네요... 하나 작성해보자면 button에 aria-label 추가해보는 것도 좋을 것 같아요!

</div>
);
};
27 changes: 27 additions & 0 deletions src/hooks/useCountdown.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,30 @@
import { useState, useEffect } from "react";

export const useCountdown = (targetDate) => {


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]);
return timeLeft;
};
12 changes: 12 additions & 0 deletions src/hooks/useErasebtn.js

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
@@ -0,0 +1,12 @@
import { useState, useEffect } from "react";

export const useErasebtn = (initialValue) => {
const [erase, setErase] = useState(initialValue);
Comment on lines +3 to +4

Choose a reason for hiding this comment

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

여러 컴포넌트에서 지우기 버튼 on/off 같은 이진 상태를 쉽게 관리 가능해서 범용성이 넓을 것 같습니다


const toggleErase = () => {

Choose a reason for hiding this comment

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

토글 기능을 활용해서 껐다 켰다 할 수 있게 만든 점이 인상 깊어요!


Comment on lines +3 to +7
Copy link

Choose a reason for hiding this comment

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

함수명이 useToggle처럼 더 일반적인 이름이면 다양한 상황에서도 쓰기 좋을 것 같아요!

setErase((prev) => !prev);
Copy link

Choose a reason for hiding this comment

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

토글을 setErase(prev => !prev) 방식으로 현재 상태를 기준으로 표현한 것 좋은 것 같아요!

Choose a reason for hiding this comment

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

setErase에 prev => !prev를 사용하는 방식이 깔끔하고 안정적인 거 같아용
이전 값을 기준으로 바꾸기 때문에, 상태가 바뀌는 중이더라도 정확하게 토글이 잘 작동한다는 점에서 잘 작성하신 거 같습니다 !

Choose a reason for hiding this comment

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

단순히 setErase(!erase) 하는 것보다 안전한 것 같아용

};

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

This file was deleted.

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};
}