Skip to content

Commit

Permalink
feat: hydrate용 클라이언트 파일 작성
Browse files Browse the repository at this point in the history
  • Loading branch information
d0422 committed May 26, 2024
1 parent 0795058 commit efa5b37
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
4 changes: 4 additions & 0 deletions apps/miniNext/public/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { makeDOM } from '../src/core/createDOM';
import App from '../src/components/App';

console.log(makeDOM(<App />));
29 changes: 29 additions & 0 deletions apps/miniNext/src/core/createDOM.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { MiniReactNode } from './jsx-runtime';

export const makeDOM = (element: string | MiniReactNode) => {
if (typeof element === 'string' || typeof element === 'number') {
//element가 text거나 number인 경우 textNode로 만든다.
return document.createTextNode(String(element));
}

const DOMElement = document.createElement(element.tagName);
if (element.props)
//props를 실제 DOM요소에 적용시킨다.
Object.keys(element.props).forEach((key) => {
if (key === 'style') {
const styleObject = element.props[key];
Object.entries(styleObject).forEach(([key, value]) => {
(DOMElement as any).style[key] = value;
});
return;
}
if (key !== 'children') (DOMElement as any)[key] = element.props[key];
});
if (element.props.children) {
//children에 대해 재귀적으로 DOM요소를 만들어 현재 요소에 붙인다.
element.props.children.forEach((child) => {
DOMElement.appendChild(makeDOM(child));
});
}
return DOMElement; //최종 생성된 DOM요소를 반환한다.;
};

0 comments on commit efa5b37

Please sign in to comment.