[4팀 이유진] Chapter 1-2. 프레임워크 없이 SPA 만들기 (2)#35
Open
Elli-Lee wants to merge 15 commits into
Open
Conversation
Author
|
|
안녕하세요, 유진 공주님. 접니다. for (const [key, value] of Object.entries(props))로 일단 props를 반복해 주면서 if (key === "style") {
Object.assign($el.style, value);
continue;
}
if (key === "className") {
$el.setAttribute("class", value);
continue;
}했습니다. if문 싫어잉~ |
|
저는 아래처럼 set과 remove로 나눠주었습니다! const updateAttributes = (target, originNewProps, originOldProps) => {
const newProps = originNewProps || {};
const oldProps = originOldProps || {};
setAttributes(target, newProps, oldProps);
removeAttributes(target, newProps, oldProps);
}; |
|
if-else로 각 속성 타입별로 다르게 처리했습니다. function updateAttributes($el, props) {
Object.entries(props).forEach(([key, value]) => {
if (key.startsWith("on")) {
// 이벤트 바인딩
} else if (key === "className") {
// class 처리
} else if (isBooleanAttribute(key)) {
// boolean 속성 처리
} else {
// 속성 제거/추가
}
});
} |
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.
과제 체크포인트
배포 링크
https://elli-lee.github.io/front_6th_chapter1-2/
기본과제
가상돔을 기반으로 렌더링하기
이벤트 위임
심화 과제
Diff 알고리즘 구현
과제 셀프회고
이번 과제의 가장 어려웠던 점은 각 함수들을 구현하는 과정에서 이 함수가 왜 필요한지, createVNode가 object를 return하는데 왜 normalizeVNode에서 한번 더 null / undefined를 처리하는지, 함수 내에서 각 분기처리를 그렇게 처리하는 이유 등등의 납득의 과정이었습니다. 과제의 목표를 명확히 이해하고 과제를 시작하고자 노력했고, 이를 위해 Virtual DOM이 필요한 이유, 동작 원리, Virtual DOM이 해결하고자 한 문제를 이해하기 위해 Vue 공식문서와 github 레포에 해당부분을 찾아보기도 하고, AI와 대화도 많이 나누는 등, 시간을 많이 투자했습니다. 이러한 노력이 과제 수행 시 덜 헤매는데 도움이 되었다고 생각합니다.
그 다음으로 어려웠던 점은 재귀의 늪이었습니다... 원하는 결과를 내고 싶어도 이를 재귀로 구현하는게 익숙하지 않아서 여러번 테스트를 돌리고 콘솔도 찍으면서 방향을 잡았습니다. 알고리즘 학습의 필요성을 느끼기도 했습니다.
여전히 코드를 깔끔하게 짜고 함수를 분리하고.. 등등은 매우 부족합니다. 그래도 최대한 이해하며 과제를 수행하려고 노력했다는 점에서는 저번주보다는 과제를 조금 더 만족스럽게 수행한 것 같습니다.
기술적 성장
새로 학습한 개념:
코드 품질
이벤트 관리자의 깔끔한 구조: WeakMap 기반의 계층적 이벤트 저장과 자동 정리
updateElement의 체계적인 분기 처리: 각 케이스별 명확한 로직 분리
가장 큰 배움이 있었던 부분
Virtual DOM의 동작 원리를 직접 구현하면서 이해하다보니, Vue등의 SPA 프레임워크들이 해결하고자 했던 문제가 조금은 보이는 것 같습니다.
이번 과제를 통해 Virtual DOM의 내부 구현 메커니즘과 diff 알고리즘의 핵심 원리에 대해 명확히 알게 되었습니다.
이번주에 실무에서 급하게 해결해야 했던 이슈 중 하나가 사실
"이미 A페이지에 위치한 사용자가 알림 등을 클릭해서 다른 페이지로 router push가 발생할 때, 만약 이동하려는 페이지가 이미 사용자가 위치한 A 페이지라면, 화면상에서는 아무런 렌더링이 발생하지 않아 사용자 입장에서는 클릭 이벤트에 버그가 있어서 router를 이동하는 것에 실패했다고 여긴다." 였습니다.
이번 과제를 수행하면서 해당 이슈를 접하니, 아무런 렌더링이 발생하지 않는 이유가 router 이동이 있어도 이동 전 후에 달라진 점이 없기 때문에 Vue의 Virtual DOM이 실제 DOM 업데이트를 진행하지 않아서임을 금방 이해할 수 있게 되었습니다.
덕분에 문제를 빨리 해결할 수 있었고, 이렇게 근본적인 동작 원리까지 아는 것의 중요성을 다시 한번 느낄 수 있었습니다.
추가 학습이 필요한 영역
과제 피드백
리뷰 받고 싶은 내용
불리언 속성 처리 방식 중 checked, readonly, selected 같은 불리언 속성을 property only로 다루고 있는데, 이런 처리 방식이 올바른 접근인지 궁금합니다.
updateAttributes에서 속성 처리 순서나 우선순위가 적절한지 궁금합니다.
className → 이벤트 → boolean → 나머지 순으로 처리하고 있는데, 이런 순서가 DOM 업데이트 시 예상치 못한 사이드 이펙트를 막는 데 적절한지 궁금합니다.
리뷰 받고 싶은 포인트를 찾기 위해 전체적으로 코드를 되돌아보다보니, createElement와 updateElement의
const booleanProps = ["checked", "disabled", "selected", "readonly", "multiple", "autofocus", "required"];
const propertyOnlyBooleanProps = ["checked", "selected"];
const propToAttributeMap = {
readOnly: "readonly",
};
부분을 공통으로 쓸 수 있을것 같아 보입니다. 과제 제출 이후 수정하겠습니다!