๋ฆฌ์กํธ ๋ธ๋ก๊ทธ์ ์๋ "React Components, Elements, and Instances" ๊ธ์ ๋ฒ์ญํ๋ฉด์ ์ ๋ฆฌํ ๊ธ์ด๋ค(์์ฃผ ๋ง์ด ํ๋ฆด ์ ์์ ์ฃผ์). ๋ฆฌ์กํธ์ ์ปดํฌ๋ํธ, ์๋ฆฌ๋จผํธ, ์ธ์คํด์ค์ ์ฐจ์ด๋ฅผ ์์๋ณด์.
๋ฆฌ์กํธ๋ฅผ ์ฒ์ ๋ฐฐ์ด๋ค๋ฉด, ์ปดํฌ๋ํธ ํด๋์ค์ ์ธ์คํด์ค๋ง ์ฌ์ฉํด๋ดค์ ๊ฒ์ด๋ค. ์๋ฅผ๋ค์ด, Button ์ปดํฌ๋ํธ๋ฅผ ํด๋์ค๋ฅผ ๋ง๋ค์ด์ ์ ์ธํ์ ๊ฒ์ด๋ค. ์ฑ์ด ๋์ํ๊ณ ์์ ๋, ์ด ์ปดํฌ๋ํธ์ ์ฌ๋ฌ ์ธ์คํด์ค๊ฐ ์คํฌ๋ฆฐ์ ์์ ๊ฒ์ด๋ฉฐ, ๊ฐ๊ฐ์ ์ธ์คํด์ค๋ ๊ฐ์์ ํ๋กํผํฐ์ ๋ก์ปฌ ์ํ๋ฅผ ๊ฐ๊ณ ์๋ค. ์ด๊ฒ์ด ์ ํต์ ์ธ object-oriented UI ํ๋ก๊ทธ๋๋ฐ์ด๋ค.
์ด๋ฌํ ์ ํต์ ์ธ UI ๋ชจ๋ธ์์ ์์ ์ปดํฌ๋ํธ ์ธ์คํด์ค๋ฅผ ์์ฑํ๊ณ ์์ ๋ ๊ฒ์ ๊ด๋ฆฌํ๋ ๊ฒ์ ์ ์ ์ผ๋ก ๋น์ ์ ๋ชซ์ด๋ค. ๋ง์ฝ Form ์ปดํฌ๋ํธ๊ฐ Button ์ปดํฌ๋ํธ๋ฅผ ๋ ๋๋งํ๊ณ ์ ํ ๋, Form ์ปดํฌ๋ํธ๋ ์ธ์คํด์ค๋ฅผ ๋ง๋ค๊ณ , ์๋ก์ด ์ ๋ณด๋ฅผ ์๋์ผ๋ก ์ ๋ฐ์ดํธํด์ผ ํ๋ค.
class Form extends TraditionalObjectOrientedView {
render() {
// Read some data passed to the view
const { isSubmitted, buttonText } = this.attrs;
if (!isSubmitted && !this.button) {
// Form is not yet submitted. Create the button!
this.button = new Button({
children: buttonText,
color: 'blue'
});
this.el.appendChild(this.button.el);
}
if (this.button) {
// The button is visible. Update its text!
this.button.attrs.children = buttonText;
this.button.render();
}
if (isSubmitted && this.button) {
// Form was submitted. Destroy the button!
this.el.removeChild(this.button.el);
this.button.destroy();
}
if (isSubmitted && !this.message) {
// Form was submitted. Show the success message!
this.message = new Message({ text: 'Success!' });
this.el.appendChild(this.message.el);
}
}
}
๊ฐ ์ปดํฌ๋ํธ ์ธ์คํด์ค๋ ๊ทธ DOM ๋ ธ๋์ ์์ ์ปดํฌ๋ํธ์ ์ธ์คํด์ค์ ๋ํ ์ฐธ์กฐ๋ฅผ ๊ฐ๊ณ ์์ด์ผ ํ๋ค. ๋ํ, ์ ์ ํ ๋์ ๊ทธ๊ฒ๋ค์ ์์ฑํ๊ณ , ์ ๋ฐ์ดํธํ๊ณ , ์์ ์ผ ํ๋ค.
์ฝ๋์ ๋ผ์ธ ์๋ ์ปดํฌ๋ํธ๊ฐ ๊ฐ๊ณ ์๋ ์ํ์ ์๊ฐ ๋์ด๋จ์ ๋ฐ๋ผ ๊ทธ๋ฆฌ๊ณ ๋ถ๋ชจ๊ฐ ์์ ์ปดํฌ๋ํธ ์ธ์คํด์ค์ ์ง์ ์ ๊ทผํจ์ ๋ฐ๋ผ ์ ์ฐจ ๋์ด๋ ๊ฒ์ด๋ค. ๋ฏธ๋์ ๊ทธ๊ฒ๋ค์ ๋ถ๋ฆฌํ๋ ๊ฒ์ ๋์ฑ ์ด๋ ต๊ฒ ๋ง๋ ๋ค.
๋ฆฌ์กํธ๋ ์ด๋ป๊ฒ ๋ค๋ฅผ๊น?
๋ฆฌ์กํธ์์๋ element๊ฐ ์ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด ๋ฑ์ฅํ๋ค.
An element is a plain object describing a component instance or DOM node and its desired properties
์๋ฆฌ๋จผํธ๋ ์ปดํฌ๋ํธ ์ธ์คํด์ค๋ DOM ๋
ธ๋๋ฅผ ๊ธฐ์ ํ๋ ์์ํ ๊ฐ์ฒด์ด๋ค. ์๋ฆฌ๋จผํธ๋ component type
(์๋ฅผ ๋ค๋ฉด, ๋ฒํผ)๊ณผ ๊ทธ properties
(์๋ฅผ ๋ค๋ฉด, color), ๊ทธ๋ฆฌ๊ณ ๊ทธ ๋ด๋ถ์ child element
์ ๋ํ ์ ๋ณด๋ง ๊ฐ๊ณ ์๋ค.
์๋ฆฌ๋จผํธ๋ ์ค์ ์ธ์คํด์ค๋ ์๋๋ค. ์๋ฆฌ๋จผํธ๋ ์คํฌ๋ฆฐ์์ ๋ณด๊ณ ์ ํ๋ ๊ฒ์ ๋ฆฌ์กํธ์๊ฒ ์๋ ค์ฃผ๋ ๋ฐฉ์์ด๋ค. ์๋ฆฌ๋จผํธ์ ์ด๋ ํ ๋ฉ์๋๋ ํธ์ถํ ์ ์๋ค. ์๋ฆฌ๋จผํธ๋ ๊ทธ์ ๋ ๊ฐ์ง ํ๋์ ๋ํ immutable description object์ด๋ค. type:(string | ReactClass)
, props: Object
์๋ฆฌ๋จผํธ์ ํ์ ์ด string์ด๋ฉด, ๊ทธ๊ฒ์ ํ๊ทธ ์ด๋ฆ์ผ๋ก DOM node๋ฅผ ๋ํ๋ด๋ ๊ฒ์ด๊ณ , props๋ ๊ทธ ์์ฑ์ ํด๋นํ๋ ๊ฒ์ด๋ค.
{
type: 'button',
props: {
className: 'button button-blue',
children: {
type: 'b',
props: {
children: 'OK!'
}
}
}
}
์์ ์๋ฆฌ๋จผํธ๋ ๋ค์ HTML์ ์์ ๊ฐ์ฒด๋ก ํํํ ๋ฐฉ์์ด๋ค.
<button class='button button-blue'>
<b>
OK!
</b>
</button>
๊ฐ ์๋ฆฌ๋จผํธ๊ฐ ์ด๋ป๊ฒ ์ค์ฒฉ๋์ด ์๋์ง๋ฅผ ์ฃผ๋ชฉํ๋ผ. ์ปจ๋ฒค์ ์ ์ํด, ์๋ฆฌ๋จผํธ ํธ๋ฆฌ๋ฅผ ๋ง๋ค๊ณ ์ถ์ ๋, ํ๋ ์ด์์ child elements๋ฅผ ๊ทธ๊ฒ์ด ์ํ ์๋ฆฌ๋จผํธ์ children prop์ผ๋ก ๋ช ์ํ๊ฒ ๋๋ค.
์ค์ํ ๊ฒ์ child์ parent ์๋ฆฌ๋จผํธ ๋ชจ๋, ๊ทธ์ description์ผ ๋ฟ์ด์ง ์ค์ ์ธ์คํด์ค๊ฐ ์๋๋ผ๋ ๊ฒ์ด๋ค. ์๋ฆฌ๋จผํธ๋ค์ ๋ง๋ค ๋, ์คํฌ๋ฆฐ์์์ ์ด๋ ํ ๊ฒ๋ ์ฐธ์กฐํ์ง ์๋๋ค. ์๋ฆฌ๋จผํธ๋ฅผ ๋ง๋ค๊ณ ๋ฒ๋ฆฌ๋ ๊ฒ์ ํฌ๊ฒ ๋ฌธ์ ๊ฐ ๋์ง ์๋๋ค.
๋ฆฌ์กํธ ์๋ฆฌ๋จผํธ๋ ์ํํ๊ธฐ ์ฌ์ฐ๋ฉฐ, ํ์ฑํ ํ์๋ ์๋ค. ์ค์ DOM elements๋ณด๋ค ํจ์ฌ ๋ ๊ฐ๋ณ๋ค. ๋ฆฌ์กํธ ์๋ฆฌ๋จผํธ๋ ๊ทธ์ ๊ฐ์ฒด์ด๊ธฐ ๋๋ฌธ์ด๋ค. (๊ฐ์ฒด๋๊น ๋ฐ๋ก ์ ๊ทผํ๋ฉด ๋๋ฏ๋ก, ์ํํ๊ธฐ ์ฝ๊ณ ํ์ฑํ ํ์๋ ์๋ค๊ณ ํ๋ ๊ฒ ๊ฐ๋ค)
์๋ฆฌ๋จผํธ์ ํ์ ์ด ์คํธ๋ง์ด ์๋๋ผ, ๋ฆฌ์กํธ ์ปดํฌ๋ํธ์ ํด๋นํ๋ ํด๋์ค๋ ํจ์์ผ ์๋ ์๋ค.
{
type: Button,
props: {
color: 'blue',
children: 'OK!'
}
}
๋ค์์ ๋ฆฌ์กํธ์ ํต์ฌ ์์ด๋์ด์ด๋ค.
An element describing a component is also an element, just like an element describing the DOM node. They can be nested and mixed with each other.
์ปดํฌ๋ํธ๋ฅผ ํํํ๋ ์๋ฆฌ๋จผํธ ๋ํ ์๋ฆฌ๋จผํธ์ด๋ค. DOM๋ ธ๋๋ฅผ ํํํ๋ ์๋ฆฌ๋จผํธ๊ฐ ์๋ฆฌ๋จผํธ์ธ ๊ฒ๊ณผ ๋ง์ฐฌ๊ฐ์ง์ธ ๊ฒ์ด๋ค. ๊ทธ๊ฒ๋ค์ ์๋ก ์ค์ฒฉ๋ ์ ์์ผ๋ฉฐ ์์ผ ์ ์๋ค.
์ด๋ฌํ ํน์ฑ์ ์ปฌ๋ฌ ์์ฑ์ ๊ฐ์ง Button
ํ์
์DangerButton
์ ์ ์ํ ์ ์๊ฒ ํ๋ค. ์ด DangerButton์ Button
์ด๋ผ๋ ๊ฒ์ด <button>
, <div>
ํน์ ๋ค๋ฅธ ์ด๋ ํ ํ๊ทธ๋ก ๋ ๋๋ง๋ ์ง์ ๋ํ ๊ฑฑ์ ์ ์ ํํ์ง ์๊ฒ ๋๋ค.
const DangerButton = ({ children }) => ({
type: Button,
props: {
color: 'red',
children: children
}
});
ํ๋์ ์๋ฆฌ๋จผํธ ํธ๋ฆฌ ์์ DOM๊ณผ ์ปดํฌ๋ํธ ์๋ฆฌ๋จผํธ๋ฅผ ๋ฏน์คํ๊ฑฐ๋ ๋งค์น์ํฌ ์๋ ์๋ค.
const DeleteAccount = () => ({
type: 'div',
props: {
children: [{
type: 'p',
props: {
children: 'Are you sure?'
}
}, {
type: DangerButton,
props: {
children: 'Yep'
}
}, {
type: Button,
props: {
color: 'blue',
children: 'Cancel'
}
}]
});
JSX๋ผ๋ฉด ๋ค์์ฒ๋ผ ํํ๋๋ค.
const DeleteAccount = () => (
<div>
<p>Are you sure?</p>
<DangerButton>Yep</DangerButton>
<Button color='blue'>Cancel</Button>
</div>
);
์ด๋ฌํ ๋ฏน์ค ์ค ๋งค์น๋ ์ปดํฌ๋ํธ๋ผ๋ฆฌ ์๋ก ๋ถ๋ฆฌ๋๊ฒ ํ๋๋ฐ ๋์์ด ๋๋ค. ์ด๋ ๊ฐ ์ปดํฌ๋ํธ๊ฐ ์กฐํฉ์ ํตํด ๋ _is-a_์ _has-a_๊ด๊ณ๊ฐ ๋ฐฐํ์ ์ผ๋ก ํํ๋ ์ ์๊ธฐ ๋๋ฌธ์ด๋ค.
- Button is a DOM
<button>
with specific properties. (is-a) - DangerButton is a Button with specific properties. (is-a)
- DeleteAccount contains a Button and a DangerButton inside a
<div>
. (has-a)
๋ฆฌ์กํธ๊ฐ ํจ์๋ ํด๋์ค ํ์ ์ ์๋ฆฌ๋จผํธ๋ฅผ ๋ณด๊ฒ ๋๋ค๋ฉด, ํด๋นํ๋ props์ ๊ดํ์ฌ ๊ทธ ์ปดํฌ๋ํธ๊ฐ ์ด๋ ํ ์๋ฆฌ๋จผํธ๋ฅผ ๋ ๋๋งํ ์ง ๋ฌผ์ด๋ณธ๋ค.
๋ค์๊ณผ ๊ฐ์ ์๋ฆฌ๋จผํธ๋ฅผ ๋ณธ๋ค๋ฉด,
{
type: Button,
props: {
color: 'blue',
children: 'OK!'
}
}
๋ฆฌ์กํธ๋ Button
์ปดํฌ๋ํธ์๊ฒ ๋ฌด์์ ๋ ๋๋งํ ๊ฒ์ธ์ง๋ฅผ ๋ฌผ์ด๋ณธ๋ค. ๊ทธ Button ์ปดํฌ๋ํธ๋ ๋ค์์ ์๋ฆฌ๋จผํธ๋ฅผ ๋ฐํํ๋ค.
{
type: 'button',
props: {
className: 'button button-blue',
children: {
type: 'b',
props: {
children: 'OK!'
}
}
}
}
๋ฆฌ์กํธ๋ ์ด ๊ณผ์ ์ ํ์ด์ง์ ๋ชจ๋ ์ปดํฌ๋ํธ์๊ฒ ๊ทธ ์๋์ ์๋ DOM ํ๊ทธ ์๋ฆฌ๋จผํธ๋ฅผ ์ ๋๊น์ง ๋ฐ๋ณตํ๋ค.
์์ ์๋ Form
์์๋ ๋ฆฌ์กํธ๋ก ๋ค์์ฒ๋ผ ํํ๋ ์ ์๋ค.
const Form = ({ isSubmitted, buttonText }) => {
if (isSubmitted) {
// Form submitted! Return a message element.
return {
type: Message,
props: {
text: 'Success!'
}
};
}
// Form is still visible! Return a button element.
return {
type: Button,
props: {
children: buttonText,
color: 'blue'
}
};
};
๋ฆฌ์กํธ ์ปดํฌ๋ํธ์๊ฒ, props๋ Input์ด๋ฉฐ, element tree๊ฐ output์ด ๋๋ค.
๋ฐํ๋ element tree๋ DOM node๋ฅผ ์ค๋ช ํ๋ ์๋ฆฌ๋จผํธ์ ๋ค๋ฅธ ์ปดํฌ๋ํธ๋ฅผ ์ค๋ช ํ๋ ์๋ฆฌ๋จผํธ๊ฐ ํฌํจ๋ ์ ์๋ค. ์ด๋ ๊ทธ ๋ด๋ถ์ DOM ๊ตฌ์กฐ์ ๊ธฐ๋์ง ์๊ณ , ๋ ๋ฆฝ์ ์ธ UI๋ฅผ ๊ตฌ์ฑํ ์ ์๊ฒ ํด์ค๋ค.
๋ฆฌ์กํธ๊ฐ instance๋ฅผ ์์ฑํ๊ณ , ์ ๋ฐ์ดํธํ๊ณ , ์์ค๋ค. ์ฐ๋ฆฌ๋ ๊ทธ ์ธ์คํด์ค๋ฅผ ์ปดํฌ๋ํธ๋ก๋ถํฐ ๋ฐํํ ์๋ฆฌ๋จผํธ๋ค๋ก ํํํ๋ฉฐ, ๋ฆฌ์กํธ๋ ๊ทธ ์ธ์คํด์ค๋ฅผ ๊ด๋ฆฌํ๋ค.
์ ์ฝ๋์์ Form
, Message
, Button
์ ๋ฆฌ์กํธ ์ปดํฌ๋ํธ์ด๋ค. ์ด๊ฒ๋ค์ ํจ์๋ก ์ฐ์ฌ์ง ์ ์๊ณ , React.Component
๋ก ๋ถํฐ ๋ด๋ ค์จ ํด๋์ค๋ก ์ฐ์ฌ์ง ์๋ ์๋ค. ์ปดํฌ๋ํธ๋ฅผ ์ ์ํ๋ ์ธ ๊ฐ์ง ๋ฐฉ์๋ค์ ๊ฑฐ์ ๋์ผํ๋ค.
// 1) As a function of props
const Button = ({ children, color }) => ({
type: 'button',
props: {
className: 'button button-' + color,
children: {
type: 'b',
props: {
children: children
}
}
}
});
// 2) Using the React.createClass() factory
const Button = React.createClass({
render() {
const { children, color } = this.props;
return {
type: 'button',
props: {
className: 'button button-' + color,
children: {
type: 'b',
props: {
children: children
}
}
}
};
}
});
// 3) As an ES6 class descending from React.Component
class Button extends React.Component {
render() {
const { children, color } = this.props;
return {
type: 'button',
props: {
className: 'button button-' + color,
children: {
type: 'b',
props: {
children: children
}
}
}
};
}
}
์ปดํฌ๋ํธ๊ฐ ํด๋์ค๋ก ์ ์๋๋ฉด, ํจ์ ์ปดํฌ๋ํธ๋ณด๋ค ์กฐ๊ธ ๋ ํ์ํํ๋ค. ํด๋์คํ ์ปดํฌ๋ํธ๋ ๋ก์ปฌ ์ํ๋ฅผ ์ ์ฅํ ์ ์์ผ๋ฉฐ, ๊ทธ์ ํด๋นํ๋ DOM node๊ฐ ์์ฑ๋๊ฑฐ๋ ์ญ์ ๋์์ ๋, ์ปค์คํ ๋ก์ง์ ์ํํ ์ ์๋ค.
ํจ์ํ ์ปดํฌ๋ํธ๋ ๋ ํ์ํํ์ง๋ง, ๋ ๊ฐ๋จํ๋ฉฐ, render()
๋ฉ์๋๋ก ํด๋์คํ ์ปดํฌ๋ํธ์ฒ๋ผ ํ๋ํ ์ ์๋ค.
ํด๋์คํ์๋ง ์๋ ํน์ง์ด ํ์ํ์ง ์๋๋ค๋ฉด, ํจ์ํ ์ปดํฌ๋ํธ๋ฅผ ์ฌ์ฉํ ๊ฒ์ ๊ถ์ฅํ๋ค.
๊ทธ๋ฌ๋, ํจ์๋ ํด๋์ค๋ ๊ทธ ๋ฐํ์ ์์ด์๋ ๋ฆฌ์กํธ์๊ฒ ์์ด์ ๋ชจ๋ ์ปดํฌ๋ํธ์ด๋ค. ๊ทธ ์ปดํฌ๋ํธ๋ค์ props๋ฅผ ์ธํ์ผ๋ก ๋ฐ์ผ๋ฉฐ, ์๋ฆฌ๋จผํธ๋ฅผ ์์ํ์ผ๋ก ๋ฐํํ๋ค.
๋ค์์ ํธ์ถํ๋ค๊ณ ํ์.
ReactDOM.render({
type: Form,
props: {
isSubmitted: false,
buttonText: 'OK!'
}
}, document.getElementById('root'));
๋ฆฌ์กํธ๋ Form ์ปดํฌ๋ํธ์๊ฒ ์ฃผ์ด์ง props๋ก ์ด๋ ํ ์๋ฆฌ๋จผํธ ํธ๋ฆฌ๋ฅผ ๋ฐํํ ๊ฒ์ธ์ง๋ฅผ ๋ฌผ์ด๋ณธ๋ค. ๋ฆฌ์กํธ๋ ๋ ๊ฐ๋จํ primitive ์ธก๋ฉด์์ ์ปดํฌ๋ํธ ํธ๋ฆฌ์ ๋ํ ์ดํด๋ฅผ ์ ์ฐจ refiningํ๊ฒ ๋๋ค.
// React: You told me this...
{
type: Form,
props: {
isSubmitted: false,
buttonText: 'OK!'
}
}
// React: ...And Form told me this...
{
type: Button,
props: {
children: 'OK!',
color: 'blue'
}
}
// React: ...and Button told me this! I guess I'm done.
{
type: 'button',
props: {
className: 'button button-blue',
children: {
type: 'b',
props: {
children: 'OK!'
}
}
}
}
์ด๊ฒ์ ๋ฆฌ์กํธ๊ฐ reconciliation
์ ํธ์ถํ๋ ๊ณผ์ ์ ์ผ๋ถ์ด๋ค. Reconciliation์ ReactDOM.render()
๋ setState()
๋ฅผ ํธ์ถํ ๋, ์์๋๋ค.
reconciliation
์ด ๋๋ ๋, ๋ฆฌ์กํธ๋ DOM tree์ ๊ฒฐ๊ณผ๋ฅผ ์๊ฒ ๋๋ฉฐ, react-dom
์ด๋ react-native
๊ฐ์ renderer๊ฐ ๋ ๋
ธ๋๋ฅผ ์
๋ฐ์ดํธํ๊ธฐ ์ํด ํ์ํ ์์ ๋ณํ์ ์งํฉ์ ์ ์ฉํ๋ค.
์ด๋ฌํ ์ ์ง์ ์ธ ์ ์ ์์ ์ ๋ฆฌ์กํธ์ฑ์ด ์ต์ ํํ๊ธฐ ์ฝ๋ค๋ ๊ฒ์ ์ด์ ๊ฐ ๋๋ค.
์ปดํฌ๋ํธ ํธ๋ฆฌ์ ์ด๋ ํ ๋ถ๋ถ์ด ๋๋ฌด ์ปค์ ธ์ ๋ฆฌ์กํธ๊ฐ ํจ๊ณผ์ ์ผ๋ก ๋ฐฉ๋ฌธํ ์ ์์ ๋, "refining"ํ๋ ์์ ์ ์คํตํ๊ณ , ํธ๋ฆฌ์ ํน์ ํํธ์ ๋ค๋ฅธ ์ ์ด ์๋์ง๋ฅผ ์ฐพ์ผ๋ฉฐ ๊ทธ props์ ๋ณํ๊ฐ ์๋์ง๋ฅผ ํ์ ํ๊ฒ ํ๋ค.
props๊ฐ immutableํ๋ค๋ฉด, ๊ทธ props๊ฐ ๋ณํํ๋์ง ํ์ ํ๋ ๊ฒ์ ๊ณ์ฐํ๋ ๊ฒ์ ๋งค์ฐ ๋น ๋ฅด๋ค. ๋ฐ๋ผ์ ๋ฆฌ์กํธ์ immutability ์์ ์ ํจ๊ป ํ ๋ ๋งค์ฐ ์ข๊ณ , ์ ์ ๋ ธ๋ ฅ์ผ๋ก๋ ํฐ ์ต์ ํ๊ฐ ์ด๋ฃจ์ด์ง๋ค.
์ด ๋ธ๋ก๊ทธ๋ ์ปดํฌ๋ํธ์ ์๋ฆฌ๋จผํธ์ ๋ํด์๋ง ๋ง์ด ๋งํ๊ณ , ์ธ์คํด์ค์ ๋ํด์๋ ๊ทธ๋ค์ง ๋ง์ด ๋งํ์ง ์์ ๊ฒ์ ์ ์ ์์์ ๊ฒ์ด๋ค. ์ธ์คํด์ค๋ ์ฌ์ค ๋๋ถ๋ถ์ ๊ฐ์ฒด ์งํฅ์ ์ธ UI ํ๋ ์์ํฌ๋ณด๋ค ๋ฆฌ์กํธ์์ ํจ์ฌ ์ค์ํ์ง ์๊ธฐ ๋๋ฌธ์ด๋ค.
ํด๋์ค๋ก ์ ์ธ๋ ์ปดํฌ๋ํธ๋ง์ด ์ธ์คํด์ค๋ฅผ ๊ฐ๊ณ ์์ผ๋ฉฐ, ์ ๋๋ก ์ธ์คํด์ค๋ฅผ ์ง์ ๋ง๋ค ์ ์๋ค. ์ด๋ ๋ฆฌ์กํธ๊ฐ ์ฌ์ฉ์๋ฅผ ์ํด ๊ทธ๋ ๊ฒ ํ ๊ฒ์ด๋ค. ๋ถ๋ชจ ์ปดํฌ๋ํธ ์ธ์คํด์ค๊ฐ ์์ ์ปดํฌ๋ํธ ์ธ์คํด์ค์ ์ ๊ทผํ๋ ๋ฉ์ปค๋์ฆ์ ์กด์ฌํ์ง๋ง, ๊ทธ๊ฒ์ ์ค์ง ํผํ ์ ์๋ ํ๋(์๋ฅผ ๋ค๋ฉด, field์ ํฌ์ปค์ค๋ฅผ ์ธํ ํ๋ ๊ฒ)์์๋ง ์ฌ์ฉ๋๋ฉฐ, ์ผ๋ฐ์ ์ผ๋ก๋ ๋ฐ๋์ ํผํด์ผ ํ๋ค.
๋ฆฌ์กํธ๋ ๋ชจ๋ ํด๋์ค ์ปดํฌ๋ํธ์ ์ธ์คํด์ค๋ฅผ ๋ง๋๋ ๊ฒ์ ๊ด๋ฆฌํ๋ฏ๋ก, ๋ฉ์๋์ ๋ก์ปฌ State๋ฅผ ๊ฐ๋ ๊ฐ์ฒด ์งํฅ์ ์ธ ๋ฐฉ์์ผ๋ก ์ปดํฌ๋ํธ๋ฅผ ์์ฑํ ์ ์๋ค. ๊ทธ๋ฌ๋ ์ธ์คํด์ค๋ ๋ฆฌ์กํธ ํ๋ก๊ทธ๋๋ฐ ๋ชจ๋ธ์์ ๊ทธ๋ค์ง ์ค์ํ์ง ์์ผ๋ฉฐ, ์ธ์คํด์ค๋ ๋ฆฌ์กํธ ๊ทธ ์์ฒด์ ์ํด์ ๊ด๋ฆฌ๋๋ค.
- Element
- ์๋ฆฌ๋จผํธ๋ DOM node๋ ๋ค๋ฅธ ์ปดํฌ๋ํธ์ ๋ํด ์คํฌ๋ฆฐ์ ๋ณด์ฌ์ก์ผ๋ฉด ํ๋ ๊ฒ์ ๋ํ๋ด๋ ์์ ๊ฐ์ฒด์ด๋ค.
- ์๋ฆฌ๋จผํธ๋ ๋ค๋ฅธ ์๋ฆฌ๋จผํธ๋ฅผ props๋ก ๊ฐ๊ณ ์์ ์ ์๋ค.
- React element๋ฅผ ๋ง๋๋ ๊ฒ์ ๋งค์ฐ ๊ฐ๋ฒผ์ด ์์ ์ด๋ค(cheap).
- ์๋ฆฌ๋จผํธ๊ฐ ์์ฑ๋๋ฉด, ์ ๋๋ก ๋ณํ๋์ง ์๋๋ค.
- ์๋ฆฌ๋จผํธ๋ฅผ ์์ฑํ๊ธฐ ์ํด์๋
React.createElement()
๋JSX
๋element factory helper
๋ฅผ ์ฌ์ฉํด์ผ ํ๋ค. ์ค์ ์ฝ๋์์ ์๋ฆฌ๋จผํธ๋ฅผ plain object๋ก ์ฐ๋ฉด ์๋๋ค. ๊ทธ์ ์ค์ ๋ก๋ ์๋ฆฌ๋จผํธ๊ฐ plain object๋ผ๋ ๊ฒ๋ง ์์๋์.
- Component
- ์ปดํฌ๋ํธ๋
render()
๋ฉ์๋๊ฐ ์๋ ํด๋์ค๋ ํจ์๋ก ์ ์๋ ์ ์๋ค. - ๋ ๋ฐฉ์ ๋ชจ๋ props๋ฅผ ์ธํ์ผ๋ก ๋ฐ์ผ๋ฉฐ, element tree๋ฅผ output์ผ๋ก ๋ฐํํ๋ค.
- ์ด๋ ํ ์ปดํฌ๋ํธ๊ฐ props๋ฅผ ์ธํ์ผ๋ก ๋ฐ์ ๋๋, ํน์ ๋ถ๋ชจ ์ปดํฌ๋ํธ๊ฐ ํ์ ๊ณผ ๊ทธ props๊ฐ ์๋ element๋ฅผ ๋ฐํํ๊ธฐ ๋๋ฌธ์ด๋ค. ์ด๋ฅผ ํตํด props๋ ๋ถ๋ชจ์์ ์์์ผ๋ก ์ ๋ฌ๋๋ค๋ ๊ฒ์ ์ ์ ์๋ค.
- ์ปดํฌ๋ํธ๋
- Instance
- ์ธ์คํด์ค๋ ํด๋์คํ ์ปดํฌ๋ํธ์์
this
๋ก ์ฐธ์กฐํ๋ ๊ฒ์ด๋ค. ์ด๋ ๋ก์ปฌ ์ํ์ ๋ผ์ดํ์ฌ์ดํด ์ด๋ฒคํธ์ ๋์ํ๋ ๊ฒ์ ์ ์ฉํ๋ค. - ํจ์ํ ์ปดํฌ๋ํธ๋ ์ธ์คํด์ค๋ฅผ ์ ํ ๊ฐ๊ณ ์์ง ์๋ค. ํด๋์ค ์ปดํฌ๋ํธ๋ง์ด ์ธ์คํด์ค๋ฅผ ๊ฐ๊ณ ์๋ค. ๊ทธ๋ฌ๋, ๋ฆฌ์กํธ๊ฐ ์ธ์คํด์ค๋ฅผ ๋ง๋๋ ๊ฒ์ ๊ด๋ฆฌํ๋ฏ๋ก, ์ง์ ์ปดํฌ๋ํธ ์ธ์คํด์ค๋ฅผ ๋ง๋ค์ง ์์๋ ๋๋ค.
- ์ธ์คํด์ค๋ ํด๋์คํ ์ปดํฌ๋ํธ์์