Skip to content
Discussion options

You must be logged in to vote

라이브코딩떄도 함께 이야기 했지만 useState를 사용하실떄는 항상 렌더링 이 필요한 ,
렌더링을 일으켜야할 상태가 필요할떄 사용하셔야 합니다

지금 같은경우는 파일을 업로드하면 렌더링을 해줘야 변경된 파일이 적용되 보일텐데요
그래서 const [file, setFile] = useState<File | null>(null); 인경우는 분명히 렌더할 필요가 있어보입니다
setFile 을 쓰는시점을 파일을 변경할태이고 그에따라 file 값을 변경시켜 컴포넌트가 변경된 file 적용되도록 렌더링 시켜야겠죵

다만

const [preview, setPreview] = useState<string | null>(null); 같은 경우에는 useState 로 선언할 이유가 보이지않습니다
preview 값에 따라 렌더가 로직상 필요해보이지않습니다

preview 값은 file 값에 따라 변하는 의존값이기에
const preview = file ? URL.createObjectURL(file) : null; 선언 해주면 될것같습니다 file 변할때마다 변경되야 하는값이니까요

아마 린트에서 에러를 띄워준것도 useEffect 내부에서 setState 를 사용하지 마라가 아니라
"너 굳이 왜 deps 에있는 file 에따라 변하는 변수 (preview) 를 useState 로 선언해서 effect 내부에서 호출해 그럼 무한루프 돌수도있을껄 ?" …

Replies: 5 comments 9 replies

Comment options

aahreum
Nov 21, 2025
Maintainer Author

You must be logged in to vote
1 reply
@aahreum
Comment options

aahreum Nov 21, 2025
Maintainer Author

Comment options

You must be logged in to vote
0 replies
Answer selected by aahreum
Comment options

You must be logged in to vote
7 replies
@tkp12345
Comment options

@tkp12345
Comment options

@aahreum
Comment options

aahreum Nov 21, 2025
Maintainer Author

@aahreum
Comment options

aahreum Nov 21, 2025
Maintainer Author

@tkp12345
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

aahreum
Nov 21, 2025
Maintainer Author

You must be logged in to vote
1 reply
@aahreum
Comment options

aahreum Nov 22, 2025
Maintainer Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
⁉️ 멘토링 질문 멘토링 질문
2 participants