Skip to content

Commit 9d42082

Browse files
김수현김수현
authored andcommitted
중복 코드 변경
1 parent 63c38e1 commit 9d42082

File tree

5 files changed

+5
-36
lines changed

5 files changed

+5
-36
lines changed

src/__tests__/advanced.test.jsx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ describe("Chapter1-2 > 심화과제 > Virtual DOM과 이벤트 관리", () => {
3030
</div>
3131
);
3232
renderElement(initialVNode, container);
33-
console.log(container.innerHTML); // 디버깅용
3433

3534
const originalH1 = container.querySelector("h1");
3635
const originalP = container.querySelector("p");
@@ -42,7 +41,6 @@ describe("Chapter1-2 > 심화과제 > Virtual DOM과 이벤트 관리", () => {
4241
</div>
4342
);
4443
renderElement(updatedVNode, container);
45-
console.log(container.innerHTML); // 디버깅용
4644

4745
expect(container.innerHTML).toBe("<div><h1>Updated Title</h1><p>Paragraph 1</p></div>");
4846
expect(container.querySelector("h1")).toBe(originalH1); // 같은 요소 참조 확인
@@ -397,7 +395,6 @@ describe("Chapter1-2 > 심화과제 > Virtual DOM과 이벤트 관리", () => {
397395
);
398396

399397
renderElement(initialVNode, container);
400-
console.log(container.innerHTML); // 디버깅용
401398
expect(container.firstChild.children.length).toBe(5);
402399

403400
// 새 상태: 2개의 자식

src/__tests__/basic.test.jsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,6 @@ describe("Chapter1-2 > 기본과제 > 가상돔 만들기 > ", () => {
196196
},
197197
},
198198
])("$name", ({ vNode, expected }) => {
199-
console.log("vNode:::::", vNode);
200-
console.log("expected:::::", expected);
201199
expect(vNode).toEqual(expected);
202200
});
203201
});

src/lib/createElement.js

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//createElement함수는 가상돔을 돔으로 변환해주는 것
2-
import { addEvent } from "./eventManager";
2+
import { updateAttributes } from "./updateElement";
33

44
export function createElement(vNode) {
55
if (vNode === null || vNode === undefined || typeof vNode === "boolean") {
@@ -18,36 +18,13 @@ export function createElement(vNode) {
1818
return fragment;
1919
}
2020

21-
// if (typeof vNode.type === "function") {
22-
// const { props = {}, children = [] } = vNode;
23-
// const nextVNode = vNode.type({ ...props, children });
24-
// return createElement(nextVNode);
25-
// }
2621
if (typeof vNode.type === "function" || (typeof vNode.type === "object" && typeof vNode.type.type === "function")) {
2722
throw new Error("컴포넌트는 createElement로 직접 처리할 수 없습니다. 먼저 normalizeVNode로 정규화하세요.");
2823
}
2924

3025
const $el = document.createElement(vNode.type);
3126

32-
Object.entries(vNode.props || {}).forEach(([key, value]) => {
33-
if (key.startsWith("on") && typeof value === "function") {
34-
const eventName = key.slice(2).toLowerCase();
35-
addEvent($el, eventName, value);
36-
} else if (key === "className") {
37-
$el.setAttribute("class", value);
38-
} else if (key === "style" && typeof value === "object") {
39-
Object.entries(value).forEach(([styleName, styleValue]) => {
40-
$el.style[styleName] = styleValue;
41-
});
42-
} else if (key.startsWith("data-")) {
43-
$el.setAttribute(key, value);
44-
} else if (typeof value === "boolean") {
45-
if (value) $el.setAttribute(key, "");
46-
// false면 아무것도 안 함
47-
} else {
48-
$el.setAttribute(key, value);
49-
}
50-
});
27+
updateAttributes($el, vNode.props || {}, {});
5128

5229
const children = Array.isArray(vNode.children) ? vNode.children : [];
5330
children.forEach((child) => {
@@ -56,5 +33,3 @@ export function createElement(vNode) {
5633

5734
return $el;
5835
}
59-
60-
//function updateAttributes($el, props) {}

src/lib/renderElement.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ export function renderElement(vNode, container) {
1818
const oldVNode = vnodeMap.get(container);
1919

2020
if (!oldVNode) {
21-
console.log("초기 렌더링");
2221
const dom = createElement(nextVNode);
2322
container.appendChild(dom);
2423
} else {

src/lib/updateElement.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { addEvent, removeEvent } from "./eventManager";
33
import { createElement } from "./createElement.js";
44

55
//DOM 요소의 속성/이벤트를 비교해서 변경, 추가, 삭제
6-
function updateAttributes(target, originNewProps, originOldProps = null) {
6+
export function updateAttributes(target, originNewProps, originOldProps = null) {
77
Object.keys(originOldProps).forEach((key) => {
88
if (!(key in originNewProps)) {
99
if (key.startsWith("on")) {
@@ -13,7 +13,7 @@ function updateAttributes(target, originNewProps, originOldProps = null) {
1313
//addEvent와 removeEvent가
1414
//실제 DOM의 addEventListener/removeEventListener까지 관리해야
1515
//이벤트가 완전히 제거됩니다.
16-
target.removeEventListener(key.slice(2).toLowerCase(), originOldProps[key]);
16+
//target.removeEventListener(key.slice(2).toLowerCase(), originOldProps[key]);
1717
} else if (key === "className") {
1818
target.removeAttribute("class");
1919
} else if (key === "checked" || key === "disabled" || key === "selected" || key === "readOnly") {
@@ -32,7 +32,7 @@ function updateAttributes(target, originNewProps, originOldProps = null) {
3232
if (key.startsWith("on") && typeof originNewProps[key] === "function") {
3333
if (originOldProps[key]) {
3434
removeEvent(target, key.slice(2).toLowerCase(), originOldProps[key]);
35-
target.removeEventListener(key.slice(2).toLowerCase(), originOldProps[key]);
35+
//target.removeEventListener(key.slice(2).toLowerCase(), originOldProps[key]);
3636
//addEvent(target, key.slice(2).toLowerCase(), originNewProps[key]);
3737
}
3838
addEvent(target, key.slice(2).toLowerCase(), originNewProps[key]);

0 commit comments

Comments
 (0)