-
Notifications
You must be signed in to change notification settings - Fork 0
[백지연] 커스텀 훅 실습 #1
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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}> | ||
| 누구게 | ||
| </button> | ||
| </div> | ||
| ); | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Math.random()을 넣어서 confetti가 화면 곳곳에서 터지는 효과가 다채롭고 재밌는 것 같아요! |
||
| }, | ||
| }); | ||
| }; | ||
| return { fire, fire2 }; | ||
| }; | ||
| 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; | ||
| }; |
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이름 상태를 관리하면서 변경될 때마다 useConfetti의 fire2 효과를 실행하는 좋은 코드인 것 같습니다 |
||
|
|
||
| const changeName = () => { | ||
| setName((prev) => (prev === "아기사자" ? "아기사자 백지연" : "아기사자")); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 상태 업데이트를 prev 기반으로 처리해서 최신 상태를 안전하게 반영시킨 점이 인상 깊습니다! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. prev를 활용해 최신 상태 기준으로 안전하게 업데이트하는 점 배워갑니다아 |
||
| }; | ||
|
|
||
| useEffect(() => { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. name 값이 바뀔 때마다 useEffect로 confetti가 실행되게 해둔 점이 좋네용!! 그리고 name변경과 confetti 실행 코드가 분리되어있어서 깔끔합니다 |
||
| fire2(); | ||
| }, [name]); | ||
thisminji marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return { | ||
| name, | ||
| changeName, | ||
| }; | ||
| }; | ||
This file was deleted.
| 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 }; | ||
| }; |
| 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 }; | ||
| }; |
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.
효과가 많아서 재미있는 것 같아요
이름이 뜬 후에는 버튼 이름을 누구게 말고 다른 걸로 바꾸면 좋을 것 같아요