-
Notifications
You must be signed in to change notification settings - Fork 0
[최선우] custom hook 실습 #7
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,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} | ||
| <button onClick={toggleErase}>눌러보기</button> | ||
|
Member
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. 깔끔해서 딱히 할말이 없네요... 하나 작성해보자면 button에 aria-label 추가해보는 것도 좋을 것 같아요! |
||
| </div> | ||
| ); | ||
| }; | ||
| 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; | ||
| }; |
|
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. 버튼을 키고 끌 수 있도록, 간단하고 직관적으로 코드 잘 작성해주신 것 같아요! |
| 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
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. 여러 컴포넌트에서 지우기 버튼 on/off 같은 이진 상태를 쉽게 관리 가능해서 범용성이 넓을 것 같습니다 |
||
|
|
||
| const toggleErase = () => { | ||
|
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. 토글 기능을 활용해서 껐다 켰다 할 수 있게 만든 점이 인상 깊어요! |
||
|
|
||
|
Comment on lines
+3
to
+7
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. 함수명이 useToggle처럼 더 일반적인 이름이면 다양한 상황에서도 쓰기 좋을 것 같아요! |
||
| setErase((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. 토글을 setErase(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. setErase에 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. 단순히 setErase(!erase) 하는 것보다 안전한 것 같아용 |
||
| }; | ||
|
|
||
| return { erase, toggleErase }; | ||
| }; | ||
This file was deleted.
| 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}; | ||
| } |
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.
erase ? … : null 로 간결하게 표현한 부분이 인상깊어요!