1. JSX
XML ๊ฐ์ ๋ฌธ๋ฒ ํ์ฅ
, JS๋ฅผ ํ์ฅํ ๋ฌธ๋ฒ
.
React.createElement์ ์ฐ๋ JavaScript ์ฝ๋๋ก ๋ณํ๋๋ค.
์ค๊ดํธ๋ฅผ ์จ์ JavaScript ์ฝ๋๋ฅผ ๊ทธ๋๋ก ์ธ ์ ์๊ณ , ๊ฒฐ๊ตญ์ JavaScript ์ฝ๋์ 1:1๋ก ๋งค์นญ๋๋ค.
babel์ ์ด์ฉํด ์ด๋ป๊ฒ ๋ณํ๋๋์ง ํ์ธํ ์ ์๋ค.
โ โPresetsโ์์ โreactโ๋ฅผ ์ฒดํฌํ๊ฑฐ๋, โPluginsโ์์ โ@babel/plugin-transform-react-jsxโ๋ฅผ ์ถ๊ฐํ๋ฉด JSX๋ฅผ ์คํํ ์ ์๋ค.
JSX ํ์ผ์ /* @jsx ์ด์ฉ๊ณ */ ์ฃผ์์ ์ถ๊ฐํ๋ฉด React.createElement ๋์ โ์ด์ฉ๊ณ โ๋ฅผ ์ฐ๊ฒ ๋๋ค.
<p>Hello, world!</p>
// ๋ณํ
React.createElement("p", null, "Hello, world!");
<Greeting name="world" />
// ๋ณํ
React.createElement(Greeting, { name: "world" });
<Button type="submit">Send</Button>
// ๋ณํ
React.createElement(Button, { type: "submit" }, "Send");
<div className="test">
<p>Hello, world!</p>
<Button type="submit">Send</Button>
</div>
//๋ณํ
React.createElement(
"div",
{ className: "test" },
React.createElement("p", null, "Hello, world!"),
React.createElement(Button, { type: "submit" }, "Send")
);
<div>
<p>Count: {count}!</p>
<button type="button" onClick={() => setCount(count + 1)}>Increase</button>
</div>
// ๋ณํ
React.createElement(
"div",
null,
React.createElement("p", null, "Count: ", count, "!"),
React.createElement("button", { type: "button", onClick: () => setCount(count + 1) }, "Increase")
);
React Element
JSX ์ฐ์ง์๊ณ React.createElement๋ฅผ ์จ์ React Element ํธ๋ฆฌ๋ฅผ ๊ฐฑ์ ํ๋๋ฐ ์ธ ์ ์๋ค. ํ์ ์๋๋ค.
class Hello extends React.Component {
render() {
return <div>Hello {this.props.toWhat}</div>;
}
}
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<Hello toWhat="World" />);
์ด๊ฑฐ๋ฅผ ์๋์ฒ๋ผ ์จ๋ ๋๋ค.
class Hello extends React.Component {
render() {
return React.createElement('div', null, `Hello ${this.props.toWhat}`);
}
}
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(React.createElement(Hello, {toWhat: 'World'}, null));
์ปดํ์ผ ์ค์ ์ ํ์ง ์์๋ ๋๋ค. ๋ณํํ ํ์๊ฐ ์์ผ๋.
JSX๋ก ํ ์ ์๋ ๊ฒ์ ๋ชจ๋ ์์ JS๋ก๋ ํ ์ ์๋ค.
JSX Runtime์ _jsx๋ ํจ์๋ฅผ, Preact๋ h๋ ํจ์๋ฅผ ์ง์ ์ง์.
๋ด๋ถ์ ์ผ๋ก createElement ๋์ ๋ฆฌ์กํธ 17๋ถํฐ ์ ๊ฑธ ์ด์ฉํด ๋์ ๊ทธ๋ฆฐ๋ค๋๊ฑฐ.
VDOM (Virtual DOM)
Virtual DOM (VDOM)์ UI์ ์ด์์ ์ธ ๋๋ โ๊ฐ์โ์ ์ธ ํํ์ ๋ฉ๋ชจ๋ฆฌ์ ์ ์ฅํ๊ณ ReactDOM๊ณผ ๊ฐ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ์ํด โ์ค์ โ DOM๊ณผ ๋๊ธฐํ
ํ๋ ํ๋ก๊ทธ๋๋ฐ ๊ฐ๋
.
์ด ๊ณผ์ ์ ์ฌ์กฐ์
์ด๋ผ๊ณ ํฉ๋๋ค.
์ฌ์กฐ์
๋ ธ๋๋ฅผ ๋ง๋ค๋ ์ด๋ ๋ฐ๊ผ๋ค, ์๋ฐ๊ผ๋ค ์ด๋ฐ๊ฑฐ ์ํด์ค๋ค. ๋ฌด์กฐ๊ฑด ์๋ ์ฝ๋๊ฐ์๊ฑฐ ์ฌ์คํ.
ํธ๋ฆฌ๋ฅผ ๋ฐ๊ฟ์ฃผ๋๊ฑฐ๊ณ ๊ทธ๊ฒ๊ณผ ํ๋ฉด์ ๊ณ์ ๋น๊ต.
์ฌ์ค์ ์ผ์ ๋ ๋ฒ ํ๋๊ฒ.
class Hello extends React.Component {
render() {
return React.createElement('div', null, `Hello ${this.props.toWhat}`);
}
}
react developer tool
์ ์ด์ฉํด์ ์ปดํฌ๋ํธ ๊ตฌ์กฐ๋ฅผ ํ์ธํ ์ ์๋ค.
์ ํํ VDOM์ ๋ณด์ฌ์ฃผ๋๊ฑด ์๋๋ค.
์ํ๋ง๋ค ๋ฆฌ๋ ๋๋๋ ์ปดํฌ๋ํธ๋ ํ์ธํ ์ ์๋ค. ๋๋ฒ๊น ์ ์ ์ฉ.
VDOM์ ์ฐ๋ ์ด์
๋ฌด์์ VDOM์ ์ฐ๋๊ฒ ๋น ๋ฅด๋ค๋ ์๊ฒฌ์ ๋ง์ง ์๋ค. (svelte์ "vdom์์จ? ์์ด๋ ์ข์๋ ?")
์ ์ง๋ณด์์ ์ ๋ฆฌํ๊ธฐ ๋๋ฌธ. ์ ์ธํ UI
๋ก ๋ ๋์ ๊ตฌ์กฐ๋ฅผ ์ ๊ณตํ๊ธฐ ๋๋ฌธ์ ์ด๋ค.
์ค์ํ ๊ฒ์, VDOM์ด ๋ฌด์์ด๊ณ ์ ์ฐ๋์ง ์๋ค๋ฉด ํ์ฉํ ์ ์๋ ์ต์ ํ ๊ธฐ๋ฒ
์ด ์๋ค๋ ๊ฒ.
Last updated