-
Notifications
You must be signed in to change notification settings - Fork 0
[이보연] 커스텀훅 실습 #11
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
base: main
Are you sure you want to change the base?
[이보연] 커스텀훅 실습 #11
Conversation
leeboyeon17
commented
Aug 2, 2025
- useWindowSize로 리팩토링
- useCountDown으로 리팩토링
- useDice 구현
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.
코드에 대한 설명 주석을 써주셔서 읽기 편한 것 같아요
주석을 조금 더 간단하게 써도 좋을 것 같아요
| @@ -0,0 +1,62 @@ | |||
| import { useState, useEffect } from "react"; //리액트에서 useState, useEffect 훅을 불러옴 | |||
|
|
|||
| //useDice라는 커스텀 훅 정의 시작 | |||
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.
필요한 주석만 다는 게 좋을 것 같아보여요!
| //사용자가 클릭시에 호출될 함수 | ||
| const rollDice = () => { | ||
| if (!rolling) setRolling(true); | ||
| //주사위가 이미 굴러가고 있으면(rolling이라면) 무시하고, | ||
| //아니라면 rolling을 true로 바꿔서 주사위 굴릴 때의 애니메이션을 시작함 | ||
| //rolling 도중 중복 클릭을 막기 위함 | ||
| }; |
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.
아이디어가 너무 귀엽네요
섹션 단위로 주석을 묶어 요약하면 더 좋을 것 같습니다😊
| //컴포넌트에서 사용할 수 있도록 현재 상태와 함수들을 외부에 전달하는 것임 | ||
| //이제 jsx파일의 컴포넌트에서 useDice()로 불러와서 쓸 수 있게 됨 | ||
|
|
||
| }; //훅 정의 종료 No newline at end of file |
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.
중복 클릭을 방지하는 rolling 상태까지 함께 고려한 점이 인상깉네요!
+주석을 간단하게 달면 더 깔끔할 것 같아요
| clearInterval(interval);// 숫자 변경(위에서 정의한 interval) 중지 | ||
| setRolling(false); //rolling 상태 false로 되돌림 | ||
| }, 800); //1000ms(1초) 후 종료 | ||
|
|
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.
const ROLL_INTERVAL = 100;
const ROLL_DURATION = 800;
이런 식으로 주사위 값 고정 시간 설정을 변수로 빼면 더 좋을 거 같아요!
| const [rolling, setRolling] = useState(false); | ||
| //주사위가 굴러가고 있는지 여부를 나타내는 상태(rolling)를 만듦 | ||
| //초기 상태는 false, 클릭 시 true로 바뀌었다가 다 굴러가면 다시 false가 됨 | ||
|
|
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.
해당 state 정의를 통해 중복 클릭 방지를 한 점이 좋아요
Soi-coding
left a comment
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.
과제하시느라 수고하셨습니다!
| //<<주사위를 굴릴 때 숫자가 반복적으로 바뀌는 애니메이션 지정>> | ||
| //0.1초마다 실행되는 반복 타이머 설정(숫자가 계속 바뀌게 하는 부분) | ||
| const interval = setInterval(() => { | ||
| const newValue = Math.floor(Math.random() * 6) +1; //1.newValue가 가질 수 있는 숫자들을 만듦 |
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.
1~6이라는 주사위 숫자를 생성하도록 const newValue = Math.floor(Math.random() * 6) + 1; 라고 잘 작성해주신 것 같아요!
| clearTimeout(timeout); | ||
| }; | ||
|
|
||
| }, [rolling]); |
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.
단순히 주사위 값을 바꾸는 것이 아니라 rolling 상태를 둔 점이 인상깊어요!
|
|
||
| //사용자가 클릭시에 호출될 함수 | ||
| const rollDice = () => { | ||
| if (!rolling) setRolling(true); |
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.
if (!rolling) 조건으로 애니메이션 중에 중복 실행을 막은 점이 좋은 것 같습니다
| //컴포넌트에서 사용할 수 있도록 현재 상태와 함수들을 외부에 전달하는 것임 | ||
| //이제 jsx파일의 컴포넌트에서 useDice()로 불러와서 쓸 수 있게 됨 | ||
|
|
||
| }; //훅 정의 종료 No newline at end of file |
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.
주석으로 코드에 대한 설명을 써주셔서 이해하기 편했습니다!