์ฃผ์ ์์
- ๋ผ์ด๋ธ๋ฌ๋ฆฌ
- ๊ฐ์DOM์ ์ํ ์ค๊ณ ๋ฐ ๊ตฌํ
- React.createElement โ ReactDOM.render โ React.createFiberFromElement
- React.createElement(element์ ๋ํ ๊ฐ์ DOM) ์ค๊ณ ๋ฐ ๊ตฌํ
- ReactDOM.render(element์ ๋ํด์ html element๋ก ๋ฐ๊พธ์ด์ฃผ๋ ๋ชจ๋) ์ค๊ณ ๋ฐ ๊ตฌํ
- React.createFiberFromElement(ReactDOM.render์์ ๋ ๋๋ง์ ์ํ ํ์ด๋ฒ ๊ฐ์ฒด) ์ค๊ณ ๋ฐ ๊ตฌํ
- ๊ฐ์DOM์ ์ํ ์ค๊ณ ๋ฐ ๊ตฌํ
ํ์ต ํค์๋
- Virtual DOM๊ณผ React Fiber
- ์์ฑ์ ํจ์
๊ณ ๋ฏผ ๋ฐ ํด๊ฒฐ๊ณผ์
React์ ํจ์ํ ์ปดํฌ๋ํธ์ ๋ํ createElement์ Fiber๊ฐ์ฒด ์ค๊ณ ๋ฐ ๊ตฌํ
React๋ ๊ธฐ์กด์ createElement๋ฅผ ํตํด ๊ฐ Element์ ๋ํ ์ ๋ณด๋ค์ ๊ฐ์ง๋ ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ค. ํ์ง๋ง ์ด ๊ฐ์ฒด๊ฐ ReactDOM.render์ ๋ฐ๋ก ์ฌ์ฉ๋๋ ๊ฒ์ด ์๋๊ณ , ReactDOM.renderํจ์ ์์์ ํ์ด๋ฒ๋ฅผ ์์ฑํ๊ณ , ๊ทธ ํ์ด๋ฒ๋ฅผ ์ฐธ์กฐํ์ฌ tree๋ฅผ ๋ง๋ ํ์ ๊ธฐ์กด์ root fiber์ ๋น๊ตํ์ฌ reconciliation ๊ณผ์ ์ ๊ฑฐ์ณ ์๋กญ๊ฒ ์ ๋ฐ์ดํธ๋ DOM์ ๋ ๋๋ง ํ์ฌ ๋ณด์ฌ์ฃผ๊ฒ ๋๋ค.
์ด๋ฌํ ๊ณผ์ ์ ๊ตฌํํ๊ธฐ์ ์์์, ํจ์ํ์ผ๋ก ๋์ด์๋ ์ปดํฌ๋ํธ์ ๊ฒฝ์ฐ์ ๋ํด์ ์ด๋ป๊ฒ jsx๋ณํ์ด ์ด๋ฃจ์ด์ง๋์ง์ ์ด์ ๋ํ ํด๊ฒฐ ๋ฐฉ์์ ์๊ฐํด๋ณด์๋ค.
/** @jsx mhreact.createElement */
import mhreact from "mhreact";
export function Test() {
return <div>์ปดํฌ๋ํธ ํ
์คํธ</div>;
}
//ํธ๋์คํ์ผ๋ง
...
/** @jsx mhreact.createElement */
import mhreact from "mhreact";
export function Test() {
return mhreact.createElement(
"div",
null,
"\uCEF4\uD3EC\uB10C\uD2B8 \uD14C\uC2A4\uD2B8"
);
}์ปค์คํ ์ปดํฌ๋ํธ๋ฅผ ๋ง๋ ํ ์ ์ฉ์์ผ ๋ณด์๋ค.
{
type: 'div',
props: { id: 'app', className: 'wow' },
children: [
{ type: [Function: Test], props: null, children: [] },
{ type: 'p', props: null, children: [Array] },
{ type: 'div', props: null, children: [Array] }
]
}ํ๋์ ์ปดํฌ๋ํธ์์ ์ด๋ ๊ฒ ์ปค์คํ ์ปดํฌ๋ํธ๋ฅผ ๋ฃ์์ ๊ฒฝ์ฐ, type์๋ function์ด ๋์ค๊ฒ ๋๋ค. ํ์ง๋ง type์ ํจ์๊ฐ ์ค๊ฒ ๋๋ค๋ฉด children์ ์์ผ ํ ์์ชฝ ์์๋ค์ ์๋ฌด๊ฒ๋ ์ค์ง ์์๋ค.
mhreact.createElement(Test, null),์ปค์คํ ์ปดํฌ๋ํธ๋ฅผ importํด์์ ์ฌ์ฉํ ๊ณณ์๋ ํจ์ ๊ทธ ์์ฒด๊ฐ ๋ค์ด๊ฐ๊ธฐ ๋๋ฌธ์ด๋ค. ๋ฐ๋ผ์ ์ด ํจ์๋ฅผ ํ์ด์ createElement์ชฝ์ ๋ค์ด๊ฐ ์ ์๋๋ก ํด์ฃผ์ด์ผ ํ๋ค.
{
type: 'div',
props: { id: 'app', className: 'wow' },
children: [
{ type: [Function: Test], props: null, children: [] },
{ type: 'p', props: null, children: [Array] },
{ type: 'div', props: null, children: [Array] }
]
}export function createElement(type, props, ...children) {
if (typeof type === "function") {
return type();
}
return new MhElement(type, props, children);
}
export function MhElement(type, props, children) {
this.type = type;
this.props = props;
this.children = children;
}
์ด๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด ํจ์ํ๊ฐ์ ๊ฒฝ์ฐ๋ ๋ฐ๋ก ํจ์๋ฅผ ๋ฏธ๋ฆฌ ์คํ์์ผ๋๊ณ createElement์ ๊ฒฐ๊ณผ๋ก ๋์ค๋ ๊ฐ์ด children๊ณผ ๊ฐ์ ์๋ง์ ์์น์ ๊ฐ์ ๋ฐ์ดํฐ๊ฐ ๋ค์ด๊ฐ ์ ์๋๋ก ํ์๋ค. ์ถ๊ฐ์ ์ผ๋ก Element๋ ์์ฑ์ ํจ์๋ฅผ ํตํด ๊ฐ์ฒดํ์์ผฐ๋ค.
export function FiberNode(type, key = null) {
this.type = type;
this.key = key;
this.state = null;
this.textNode = null;
this.props = null;
this.index = null;
//fibers
this.child = null;
this.sibling = null;
this.return = null;
}์ด๋ฅผ ํตํด Fiber ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ ๋ก์ง๊ณผ Fiber ๊ฐ์ฒด ์์ฒด ๋ํ ์์ฑ์ ํจ์๋ก ๊ตฌํํด์ฃผ์๋ค. ์ด๊น๊ฐ์ด ๋๋ถ๋ถ null๋ก ๋์ด์๋ ์ด์ ๋ MhElement ๊ฐ์ฒด์ ๋ํด ์ฌ๊ท์ ์ผ๋ก ์คํํ๋ฉด์ Fiber๊ฐ์ฒด๋ฅผ ์์ฑํ๋ ๊ณผ์ ์์ ๊ทธ๋๊ทธ๋๋ง๋ค element๋ฅผ ๋ณด๊ณ ๊ฐ์ฒด๋ฅผ ์ ๋ฐ์ดํธ์์ผ์ฃผ์ด์ผ ํ๊ธฐ ๋๋ฌธ์ด๋ค.
๋ฆฌ๋ทฐ ์์ฒญ
์๋ ํ์ธ์. ๋ฉํ ๋. ๊ณ ์ ๋ง์ผ์ญ๋๋ค! ์ด๋ฒ ๋ฆฌ๋ทฐ์์๋ ์์ง ํ์ตํ ๊ฒ๋ค์ด ๋ง์ ํ์ต์ ๋ง์ ์๊ฐ์ ์์ ํ์ ๋ง์ด ํ์ง ๋ชปํ์ต๋๋ค,, ๋ค์ ๋ฆฌ๋ทฐ์์๋ ๋ณด๋ค ์ง๋ฌธ์ ์ ๋ฆฌํด์ ๋ง์๋๋ฆฌ๊ฒ ์ต๋๋ค! ํธํ๊ฒ ๋ด์ฃผ์๊ณ ์ ์ฒด์ ์ธ ํผ๋๋ฐฑ๋ง ํด์ฃผ์๋ฉด ๊ฐ์ฌํ๊ฒ ์ต๋๋ค :)