-
Notifications
You must be signed in to change notification settings - Fork 42
[3팀 이윤지] Chapter 4-2. 코드 관점의 성능 최적화 #19
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
Open
yoonhihi97
wants to merge
28
commits into
hanghae-plus:main
Choose a base branch
from
yoonhihi97:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
과제 체크포인트
과제 요구사항
배포 후 url 제출
https://yoonhihi97.github.io/front_7th_chapter4-2/
API 호출 최적화(
Promise.all이해)SearchDialog 불필요한 연산 최적화
SearchDialog 불필요한 리렌더링 최적화
시간표 블록 드래그시 렌더링 최적화
시간표 블록 드롭시 렌더링 최적화
과제 셀프회고
기술적 성장
1. Context 분리의 중요성
문제: 하나의 스케줄을 드래그하면 6개 테이블의 139개 DraggableSchedule이 모두 리렌더링됨
원인: 모든 테이블이 하나의 공유된 DndContext를 사용.
useDraggable훅이 내부적으로useContext를 사용하기 때문에 DndContext 상태 변경 시 모든 구독 컴포넌트가 리렌더링됨.해결: dnd-kit 공식 문서에 따르면 DndContext를 중첩하면 각 context 내의 draggable/droppable 노드만 서로 접근 가능. 즉, 각 테이블에 독립적인 DndContext를 적용하면 다른 테이블에 영향을 주지 않음.
결과: schedule-1 드래그 시 schedule-2~6은 리렌더링 없음
2. 같은 테이블 내 다른 아이템 리렌더링 방지
문제: 스케줄을 드롭하면 이동한 아이템뿐 아니라 같은 테이블의 다른 아이템들도 모두 리렌더링됨
원인:
DraggableSchedule컴포넌트가useDraggable훅을 사용useDraggable은 내부적으로useContext(DndContext)를 호출useContext를 사용하는 컴포넌트는memo로 감싸도 context 값이 바뀌면 무조건 리렌더링됨해결: 컴포넌트를 2개로 분리
DraggableSchedule: useDraggable 훅만 사용 (리렌더링 불가피)ScheduleItem: 순수 UI 컴포넌트, memo 적용 (props 안 바뀌면 리렌더링 안됨)결과: 드롭 시 위치가 바뀐 아이템 1개만 실제 DOM 업데이트
코드 품질
Context Query/Command 패턴 분리
코치님 Q&A 세션에서 배운 패턴인데, 기존에 제가 사용하던 방식과 비교하면서 정리해봤습니다.
기존에 사용하던 Context 방식 (Before)
문제점: SearchDialog는
setSchedulesMap만 필요한데,schedulesMap이 바뀔 때마다 리렌더링됩니다. Context의 value 객체가 바뀌면 그걸 구독하는 모든 컴포넌트가 리렌더링되기 때문입니다.Query/Command 패턴 (After)
읽기(Query)와 쓰기(Command)를 별도의 Context로 분리합니다.
왜 이게 되는가?
useState의 setter 함수는 stable identity를 가집니다 (공식 문서: "The set function has a stable identity")ScheduleCommandContext의 value는 절대 바뀌지 않습니다배운 점: 이 패턴을 몰랐을 때는 "Context 쓰면 리렌더링 많이 되니까 전역 상태관리 써야지"라고 생각했는데, Context만으로도 충분히 최적화할 수 있다는 걸 알게 됐습니다. 앞으로 Context 설계할 때 기본으로 적용할 것 같습니다.
학습 효과 분석
이번 과제에서 가장 크게 배운 점은 "렌더링이 왜 일어나는지"를 정확히 이해하는 것이었습니다.
처음에는
memo로 감싸면 리렌더링이 막힐 줄 알았는데,useContext를 쓰는 순간 memo가 무력화된다는 걸 몰랐습니다.왜 이런 일이 발생할까?
React의 리렌더링은 크게 3가지 경우에 발생합니다:
React 공식 문서에서도 이렇게 명시하고 있습니다:
즉,
memo는 props 변경만 체크합니다. context는 props가 아니라 컴포넌트 내부에서 구독하는 값이기 때문에, context가 바뀌면 memo와 상관없이 리렌더링됩니다.해결법: useContext를 쓰는 컴포넌트와 실제 UI를 분리해야 memo 효과를 볼 수 있음
이렇게 분리하면:
DraggableSchedule: context 변경 시 함수는 호출되지만, props 계산만 하고 끝ScheduleItem: props(left, top 등)가 안 바뀌면 리렌더링 안됨 → 실제 DOM 업데이트 없음리뷰 받고 싶은 내용