์ง๊ธ๊น์ง ๋จ์ผ ์ปดํฌ๋ํธ์ ๋ฐ์ดํ๋ฅผ ํ์ํ๊ณ ์ ์ ์ ๋ ฅ์ ์ฒ๋ฆฌํ๋ ๋ฐฉ๋ฒ์ ๋ํด ์์๋ณด์๋ค. ๋ค์์๋ React์ ๊ฐ์ฅ ์ข์ ํน์ง์ค์ ํ๋๋ฅผ ์ดํด๋ณด์. ์ฆ ๊ตฌ์ฑ๊ฐ๋ฅ์ฑ.
์ ์ ์๋ ์ธํฐํ์ด์ค๋ก ๋ค๋ฅธ ์ปดํฌ๋ํธ๋ฅผ ์ด์ฉํ๋ ๋ชจ๋๋ผ ์ปดํฌ๋ํธ๋ฅผ ๋ง๋ค๋ฉด, ํจ์์ ํด๋์ค๋ฅผ ์ฌ์ฉํ๋ฉด์ ์ป๋ ๋๊ฐ์ ์ด์ ์ ๋๋ฆด ์๊ฐ ์๋ค. ํนํ ์ฑ์ ๊ด์ฌ์ฌ๋ฅผ ๋ถ๋ฆฌํ ์ ํด์ ์๋ก์ด ์ปดํฌ๋ํธ๋ฅผ ๋ง๋ค๊ธฐ๋ง ํ๋ฉด ๋๋ค. ๋น์ ์ ์ดํ๋ฆฌ์ผ์ด์ ์ ์ํ ์ปค์คํ ์ปดํฌ๋ํธ๋ฅผ ๋ง๋ค์ด์ ๋น์ ์ ๋๋ฉ์ธ์ ๊ฐ์ฅ ์ ๋ง๋ UI๋ฅผ ํํํ ์ ์๋ค.
ํ๋กํ์ผ ์ฌ์ง๊ณผ ์ ์ ์ด๋ฆ์ ๋ณด์ฌ์ฃผ๋ ๊ฐ๋จํ ์๋ฐํ ์ปดํฌ๋ํธ๋ฅผ ๋ง๋ค์ด ๋ณด์.
var Avatar = React.createClass({
render: function() {
return (
<div>
<ProfilePic username={this.props.username} />
<ProfileLink username={this.props.username} />
</div>
);
}
});
var ProfilePic = React.createClass({
render: function() {
return (
<img src={'http://graph.facebook.com/' + this.props.username + '/picture'} />
);
}
});
var ProfileLink = React.createClass({
render: function() {
return (
<a href={'http://www.facebook.com/' + this.props.username}>
{this.props.username}
</a>
);
}
});
React.render(
<Avatar username="pwh" />,
document.getElementById('example')
);Avatar ์ธ์คํด์ค๋ ProfilePic๊ณผ ProfileLink ์ธ์คํด์ค๋ฅผ ์์ ํ๋ค. React์์๋ ๋ค์ ์ปดํฌ๋ํธ์ props๋ฅผ ์ค์ ํ๋ ์ปดํฌ๋ํธ๊ฐ ์์ ์์ด๋ค. ํ์์ ์ผ๋ก๋ ์ปดํฌ๋ํธ x๊ฐ ์ปดํฌ๋ํธ y์ render()ํจ์์์ ๋ง๋ค์ด์ง๋ค๋ฉด, y๋ x๋ฅผ ์์ ํ๋ค๊ณ ๋งํ๋ค. ์์์ ์ธ๊ธํ๋ฏ์ด, ์ปดํฌ๋ํธ๋ ์์ ์ props๋ฅผ ๋ณ๊ฒฝํ ์ ์๋ค. props๋ ํญ์ ์์ ์๊ฐ ์ค์ ํ ๊ฐ์ผ๋ก ๊ณ ์ ๋์ด ์๋ค. ์ด๋ ๊ฒ ํด์ UI์ ์ผ๊ด์ฑ์ด ์ ์ง๋๋ค.
์์ ์ ๊ด๊ณ(owner-ownee)์ ๋ถ๋ชจ-์์(parent-child)๊ด๊ณ๋ฅผ ๊ตฌ๋ถํ๋ ๊ฒ์ด ์ค์ํ๋ค. ์์ ์ ๊ด๊ณ๋ฅผ React์์ ์ข ํน๋ณํ ๋ฐ๋ฉด ๋ถ๋ชจ-์์ ๊ด๊ณ๋ ์ผ๋ฐ์ ์ธ DOM ๊ด๊ณ์ด๋ค. ์์ ์์์ Avatar๋ ProfilePic๊ณผ ProfileLink๋ฅผ ์์ ํ์ง๋ง, div๋ ProfilePic๊ณผ ProfileLink์ ๋ถ๋ชจ์ด๋ค.
React ์ปดํฌ๋ํธ๋ฅผ ๋ง๋ค ๋, ์ถ๊ฐ์ ์ธ React Component๋ Javascript ํํ์ ์์ ํ๊ทธ์ ์ข ๋ฃ ํ๊ทธ์ฌ์ด์ ๋ฃ์ ์ ์๋ค.
<Parent><Child /></Parent>์์์ ์ฝ๊ธฐ ์ํด ๋ถ๋ชจ๋ this.props.children์ด๋ผ๋ ํน๋ณ prop์ ์ฌ์ฉํ ์ ์๋ค.
์ด๋ณด๋ค๋ React.Children utilities ๋ฅผ ์ฌ์ฉํ๋ผ.
์์ ์ผ์น(Child Reconciliation)
...๊ณ์