Skip to content
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

[15팀 정채은] [Chapter 1-2] 프레임워크 없이 SPA 만들기 #29

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 39 additions & 3 deletions src/lib/createElement.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,41 @@
import { addEvent } from "./eventManager";
export function createElement(vNode) {
if (vNode === null || vNode === undefined || typeof vNode === "boolean") {
return document.createTextNode("");
}

if (typeof vNode === "string" || typeof vNode === "number") {
return document.createTextNode(vNode);
}

export function createElement(vNode) {}
if(Array.isArray(vNode)){
const fragment = document.createDocumentFragment();
vNode.forEach((node) =>{
const doms = createNodeWithChildren(node);
fragment.appendChild(doms);
})
return fragment;
}else{ // typeof "Object"
return createNodeWithChildren(vNode);
}
}

function updateAttributes($el, props) {}
function updateAttributes($el, props) {

}

function createNodeWithChildren(node){
const domX = document.createElement(node.type);
//props 삽입
for(let propK in node.props){
if (propK === 'className') {
domX.setAttribute('class', node.props[propK]);
} else {
domX.setAttribute(propK, node.props[propK]);
}
}
//child 삽입
node.children.forEach((child) =>{
domX.appendChild(createElement(child));
})
return domX;
}
13 changes: 12 additions & 1 deletion src/lib/createVNode.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
export function createVNode(type, props, ...children) {
return {};
return {
type,
props,
// 기존
// children: (children.flat(Infinity)).filter((childX) => !!childX)
// 개선
//개선1 : children: (children.flat(Infinity)).filter(Boolean) -> 이 경우 0을 falsy로 인식하여 리턴 제외함
//개선2 : 최종
children: children
.flat(Infinity)
.filter((childX) => (childX === 0 ? true : Boolean(childX))), // 완전 평탄화

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

와 채은님 이 조건문 저한텐 너무 신박한것같아요..! 0이어도 true, 0 아니면 Boolean 타서 truthy 필터 🙊

};
}
37 changes: 36 additions & 1 deletion src/lib/normalizeVNode.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,38 @@
export function normalizeVNode(vNode) {
return vNode;
// null, undefined, boolean 값은 빈 문자열로 변환되어야 한다
if (vNode === null) {
return "";
}
switch (typeof vNode) {
case "boolean":
case "undefined":
return "";

case "number":
case "string":
return `${vNode}`;

case "function": // vNode가 함수일 경우 해당 함수를 호출해서 반환값을 얻되, 이 반환값이 또 다른 vNode일 가능성이 있으므로 재귀적으로 표준화.
const result = vNode();
return normalizeVNode(result); // 함수가 반환한 결과 normalize

default:

if(typeof vNode.type === "function"){

const { type, props, children } = vNode;
// const param = { props: vNode.props , children: vNode.children }

const resultOfFunction = vNode.type({ ...props, children }); // 함수 호출
// const resultOfFunction = vNode.type(param); // 이건 왜 안될까...
vNode = normalizeVNode(resultOfFunction); // 결과 표준화

}
if (vNode.children && Array.isArray(vNode.children)) {
vNode.children = vNode.children
.map((child) => normalizeVNode(child))
.filter((childN)=> !!childN)
}
return vNode;
}
}
Loading