์ฃผ์ ์์
- ๋ผ์ด๋ธ๋ฌ๋ฆฌ
- DOM์ createRoot ์ค๊ณ ๋ฐ ๊ตฌํ(์ํธ๋ฆฌ ํฌ์ธํธ)
- reconciliation(์๋ก์ด Fiber์ ๊ธฐ์กด Fiber ์ฌ์กฐ์ ) ํจ์ ์ค๊ณ ๋ฐ ๊ตฌํ
- diff ์๊ณ ๋ฆฌ์ฆ์ ์์ง..
- render ํจ์ ์์
- ์๋ก์ด ํ์ด๋ฒ๋ฅผ ๋ง๋ค๊ณ , ์ฌ์กฐ์ ์ด ํ์ํ ๊ฒฝ์ฐ ์ฌ์กฐ์ ํ ํ์ DOM์ ๋ ๋๋งํ๋ ๊ณผ์ ์ผ๋ก ์์
- ๊ธฐ์กด renderํจ์๋ createHTMLElementFromFiber ํจ์๋ก, ์ค์ง HTML Element๋ง ๋ง๋๋ ๊ณผ์ ๋ง ์ฑ ์์ง ์ ์๋๋ก ๋ฆฌํฉํ ๋ง
- rootFiber ์ค๊ณ ๋ฐ ๊ตฌํ
- createRootFiber๋ฅผ ํตํด ์๋ก์ด rootFiber ์์ฑ ์ค๊ณ ๋ฐ ๊ตฌํ
- getRootFiber๋ฅผ ํตํด ์ฑ๊ธํค ํจํด์ผ๋ก ๋ rootfiber๋ฅผ ๊ฐ์ ธ์ฌ ์ ์๋๋ก ๊ตฌ์ฑ
- patchRootFiber๋ฅผ ํตํด ์ฌ์กฐ์ ์ด ์๋ฃ๋ ํ์ด๋ฒ๋ฅผ ๊ธฐ์กด์ rootFiber์์ ์ ๋ฐ์ดํธ์์ผ์ฃผ๋ ๋ก์ง ์ค๊ณ ๋ฐ ๊ตฌํ
ํ์ต ํค์๋
- ์ค๋๋ Virtual DOM๊ณผ React Fiber..
๊ณ ๋ฏผ ๋ฐ ํด๊ฒฐ๊ณผ์
๋ ๋๋ง ๊ณผ์ ์ ๋ฐ๋ฅธ ํจ์ ์ค๊ณ
jsx์์ babel์ ํตํด ํธ๋์คํ์ผ๋งํ๋ ๊ณผ์ ์์ ๊ฐ๊ฐ์ element์ ๋ํด์ createElement๊ฐ ์คํ๋๊ณ , ์์ฑ๋ ๊ฒฐ๊ณผ๋ฌผ์ ํ ๊ฐ์ฒด๊ฐ ๋์ค๊ฒ ๋๋ค.
๋ฌธ์ ๋ ์ด๋ฌํ element ๊ฐ์ฒด์ ๋ํด์ ์ธ์ ์ด๋๋ฅผ fiber๊ฐ์ฒด๋ก ๋ณํํ๊ณ ๊ณ์ํด์ ๋ ๋๋ง ๋ก์ง์ ๊ด๋ฆฌํ๋๊ฐ์ ๋ํ ๊ณ ๋ฏผ์ด ๋ค์ฃฝ๋ฐ์ฃฝ์ด๋ผ ๋ค์๊ธ ๋ฆฌ์กํธ์ ๊ณต์๋ฌธ์๋ฅผ ๋ณด์๋ค.
React์ ๊ณต์ ๋ฌธ์์์ ๋ณด๋ฉด ๊ทผ๋ณธ์ ์ธ entry point๊ฐ ๋๋ ๊ฒ์ index.js์ด๋ค. index.js์ index.html์ ํตํด ๊ฐ์ฅ ๊ธฐ๋ณธ์ ์ธ ๋ผ๋๋ง์ index.html์ ๋ง๋ค์ด ๋๊ณ , index.js๊ฐ ํด๋น root node๋ฅผ ๋ฐ์ ์ด๋ฅผ DOM ์์ฒด์ root๋ก ์ง์ ํ๋ค.
๊ทธ๋์ ๋๋ index.js์ root๋ ๋ธ๋ผ์ฐ์ ์์ฒด์ root๊ฐ ๋๋ฉฐ Fiber์ root๊ฐ ๋๋ ๊ณณ์ ํด๋น root id๋ฅผ ๊ฐ์ง element ์์ชฝ์ ์์๊ฐ Fiber์ root๊ฐ ๋๋ค๊ณ ์ดํดํ๋ค.
SPA(Single Page Application)์ ํต์ฌ์, ํ์ด์ง์ ์ด๋ ์์ด ๊น๋ํ๊ฒ ๋ง์น โ์ฑโ์ฒ๋ผ ์ด๋ํ๋ ๊ฒ์ด๋ค. ์ด๋ฅผ ์ํด์๋ ๊ทผ๋ณธ์ ์ธ html ์ ์ฒด์ ์ธ template ์์ฒด๋ฅผ ๊ฐ์๋ผ์ฐ๋ ๊ฒ์ด ์๋๋ผ, html ๋ผ๋ ์์ฒด๋ ๊ณ ์ ์ ์ผ๋ก ๋๋ html template ์์์์ body ๋ถ๋ถ๋ง ๊ณ์ํด์ DOM์ ์ฌ๊ตฌ์ฑํ๊ณ ๊ฐ์๋ผ์ฐ๋ ๊ฒ์ด๋ผ๊ณ ์๊ฐํ๋ค. ๋ฐ๋ผ์ root์ FiberRoot๋ฅผ ๋ฐ๋ก ๋๋ ๊ฒ์ด ๋ณด๋ค ์ค๋๋ ฅ์ด ์๋ค๊ณ ์๊ฐํ๋ค.
import { render } from "./DomRender";
let instance = null;
export function Root(rootNode) {
if (instance) return instance;
this.rootNode = rootNode;
instance = this;
}
Root.prototype.render = (element) => {
render(element);
};
export function createRoot(domNode) {
return new Root(domNode);
}๋ฐ๋ผ์ ๋๋ html ๋ผ๋๊ฐ ๋๋ root์ ๊ฒฝ์ฐ๋ ์ฑ๊ธํค ํจํด์ ์ด์ฉํด ํญ์ ๊ฐ์ html template์ ์ ์งํ๋๋ก ํ์ผ๋ฉฐ, renderํจ์๋ฅผ ํตํด App, ์ฆ ๋ฆฌ์กํธ ์์ ์ํธ๋ฆฌ ํฌ์ธํธ๋ฅผ ๋ ๋๋ง์ํค๋ renderํจ์๋ฅผ Root instance์ prototype์ผ๋ก ๋ฑ๋กํด๋์๋ค.
let rootFiber = null;
export function createRootFiber(rootFiber) {
if (rootFiber) return rootFiber;
return new RootFiber(rootFiber);
}
export function patchRootFiber(patchedRootFiber) {
if (rootFiber) return rootFiber;
rootFiber.patch(patchedRootFiber);
return rootFiber;
}
export function RootFiber(rootFiberInstance) {
if (rootFiber) return rootFiber;
this.rootFiberInstance = rootFiberInstance;
rootFiber = this;
}
RootFiber.prototype.patch = (patchedRootFiber) => {
this.rootFiberInstance = patchedRootFiber;
rootFiber = this;
};rootFiber์ ๊ฒฝ์ฐ์๋ ๋ฆฌ์กํธ ์์์ ์ค์ง์ ์ผ๋ก ๋ ๋๋ง์ด ๋ฐ๋ณต์ ์ผ๋ก ์ด๋ฃจ์ด์ง๋ body ์์ ์์๋ค์ Fiber ๊ฐ์ฒด๋ก ๋ง๋ค์ด๋์ ์ธ์คํด์ค์ด๋ค. ์ฌ์กฐ์ ์ด ์ผ์ด๋ ๊ฒฝ์ฐ patch๋ฅผ ํตํด RootFiber๊ฐ์ฒด๋ฅผ ๊ฐ์๋ผ์ด๋ค.
์ด๋ ๊ฒ root๋ฅผ DOMRoot์ FiberRoot๋ฅผ ๋๊ฐ ๋ชจ๋ ์ฑ๊ธํค์ผ๋ก ๊ตฌํํ ๋ค์, ์ด๋ฅผ ์ค์ง์ ์ผ๋ก ์ ์ฉํด๋ณด์๋ค.
//index.js
import * as mhReact from "mhreact";
import App from "./app.jsx";
const root = mhReact.createRoot(document.getElementById("root"));
root.render(<App />);index.js๋ ๊ฐ์ฅ ๊ทผ๋ณธ์ ์ธ ๋ผ๋๋ฅผ ๋ง๋ค์ด์ฃผ๋ ์ญํ ์ ํ๋ค.
react ๊ณต์๋ฌธ์์ ๋์๋ Index์ ์ญํ ์ฒ๋ผ, root๋ผ๋ id๋ฅผ ๊ฐ์ง element๋ฅผ ๋ฐ์์ ํด๋น element๋ฅผ root๋ก ์ค์ ํ์ฌ ํด๋น element ์์์ ๊ณ์ํด์ DOM์ ๊ฐ์๋ผ์ธ ์ ์๋๋ก ํ์๊ณ , ์ค์ง์ ์ธ ์์ชฝ ์์์ ์ํธ๋ฆฌํฌ์ธํธ๋ app.jsx๋ก ์ค์ ํ์๋ค.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body id="root"></body>
<script type="module" src="./bundle.js"></script>
</html>index.html์ ์ ๋ง ๊ธฐ๋ณธ์ ์ธ ๋ผ๋๋ง ๊ฐ์ง๊ณ ์์ผ๋ฉด์ bodyํ๊ทธ๋ฅผ fiberRoot๊ฐ ๋ค์ด๊ฐ ๋ฆฌ์กํธ์ ์ํธ๋ฆฌํฌ์ธํธ๋ก ์ค์ ํด๋์๋ค.
์ด ์ํธ๋ฆฌํฌ์ธํธ๋ script์์ ๋ฒ๋ค๋ง๋ ์ฝ๋๋ฅผ ํตํด index.js๋ฅผ ์คํ์์ผ body ์์ชฝ์์ <App/> ์ํธ๋ฆฌ ํฌ์ธํธ๋ฅผ ๋ ๋๋ง ์ํฌ ์ ์๋๋ก ํด์ฃผ์๋ค.