Components and Props
์ปดํฌ๋ํธ๋ฅผ ํตํด UI๋ฅผ ์ฌ์ฌ์ฉ ๊ฐ๋ฅํ ๊ฐ๋ณ์ ์ธ ์ฌ๋ฌ ์กฐ๊ฐ์ผ๋ก ๋๋๊ณ , ๊ฐ ์กฐ๊ฐ์ ๊ฐ๋ณ์ ์ผ๋ก ์ดํด๋ณผ ์ ์์ต๋๋ค. ์ด ํ์ด์ง์์๋ ์ปดํฌ๋ํธ์ ๊ฐ๋ ์ ์๊ฐํฉ๋๋ค. ์์ธํ ์ปดํฌ๋ํธ API ๋ ํผ๋ฐ์ค๋ ์ฌ๊ธฐ์์ ํ์ธํ ์ ์์ต๋๋ค.
๊ฐ๋ ์ ์ผ๋ก ์ปดํฌ๋ํธ๋ JavaScript ํจ์์ ์ ์ฌํฉ๋๋ค. โpropsโ๋ผ๊ณ ํ๋ ์์์ ์ ๋ ฅ์ ๋ฐ์ ํ, ํ๋ฉด์ ์ด๋ป๊ฒ ํ์๋๋์ง๋ฅผ ๊ธฐ์ ํ๋ React ์๋ฆฌ๋จผํธ๋ฅผ ๋ฐํํฉ๋๋ค.
ํจ์ ์ปดํฌ๋ํธ์ ํด๋์ค ์ปดํฌ๋ํธ
์ปดํฌ๋ํธ๋ฅผ ์ ์ํ๋ ๊ฐ์ฅ ๊ฐ๋จํ ๋ฐฉ๋ฒ์ JavaScript ํจ์๋ฅผ ์์ฑํ๋ ๊ฒ์ ๋๋ค.
function Welcome(props) {
return <h1>Hello, {props.name}</h1>;
}
์ด ํจ์๋ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ง ํ๋์ โpropsโ (props๋ ์์ฑ์ ๋ํ๋ด๋ ๋ฐ์ดํฐ์ ๋๋ค) ๊ฐ์ฒด ์ธ์๋ฅผ ๋ฐ์ ํ React ์๋ฆฌ๋จผํธ๋ฅผ ๋ฐํํ๋ฏ๋ก ์ ํจํ React ์ปดํฌ๋ํธ์ ๋๋ค. ์ด๋ฌํ ์ปดํฌ๋ํธ๋ JavaScript ํจ์์ด๊ธฐ ๋๋ฌธ์ ๋ง ๊ทธ๋๋ก โํจ์ ์ปดํฌ๋ํธโ๋ผ๊ณ ํธ์นญํฉ๋๋ค.
๋ํ ES6 class๋ฅผ ์ฌ์ฉํ์ฌ ์ปดํฌ๋ํธ๋ฅผ ์ ์ํ ์ ์์ต๋๋ค.
class Welcome extends React.Component {
render() {
return <h1>Hello, {this.props.name}</h1>;
}
}
React์ ๊ด์ ์์ ๋ณผ ๋ ์ ๋ ๊ฐ์ง ์ ํ์ ์ปดํฌ๋ํธ๋ ๋์ผํฉ๋๋ค.
class๋ ๋ช ๊ฐ์ง ์ถ๊ฐ ๊ธฐ๋ฅ์ด ์์ผ๋ฉฐ ์ด์ ๋ํด์๋ ๋ค์ ์ฅ์์ ์ค๋ช ํฉ๋๋ค. ๊ทธ๋๊น์ง๋ ๊ฐ๊ฒฐ์ฑ์ ์ํด ํจ์ ์ปดํฌ๋ํธ๋ฅผ ์ฌ์ฉํ๊ฒ ์ต๋๋ค. ํจ์ ์ปดํฌ๋ํธ์ ํด๋์ค ์ปดํฌ๋ํธ ๋ ๋ค ๋ช ๊ฐ์ง ์ถ๊ฐ ๊ธฐ๋ฅ์ด ์์ผ๋ฉฐ ์ด์ ๋ํด์๋ ๋ค์ ์ฅ์์ ์ค๋ช ํฉ๋๋ค.
์ปดํฌ๋ํธ ๋ ๋๋ง
์ด์ ๊น์ง๋ React ์๋ฆฌ๋จผํธ๋ฅผ DOM ํ๊ทธ๋ก ๋ํ๋์ต๋๋ค.
const element = <div />;
React ์๋ฆฌ๋จผํธ๋ ์ฌ์ฉ์ ์ ์ ์ปดํฌ๋ํธ๋ก๋ ๋ํ๋ผ ์ ์์ต๋๋ค.
const element = <Welcome name="Sara" />;
React๊ฐ ์ฌ์ฉ์ ์ ์ ์ปดํฌ๋ํธ๋ก ์์ฑํ ์๋ฆฌ๋จผํธ๋ฅผ ๋ฐ๊ฒฌํ๋ฉด JSX ์ดํธ๋ฆฌ๋ทฐํธ์ ์์์ ํด๋น ์ปดํฌ๋ํธ์ ๋จ์ผ ๊ฐ์ฒด๋ก ์ ๋ฌํฉ๋๋ค. ์ด ๊ฐ์ฒด๋ฅผ โpropsโ๋ผ๊ณ ํฉ๋๋ค.
๋ค์์ ํ์ด์ง์ โHello, Saraโ๋ฅผ ๋ ๋๋งํ๋ ์์์ ๋๋ค.
function Welcome(props) { return <h1>Hello, {props.name}</h1>;
}
const element = <Welcome name="Sara" />;ReactDOM.render(
element,
document.getElementById('root')
);
์ด ์์์์๋ ๋ค์๊ณผ ๊ฐ์ ์ผ๋ค์ด ์ผ์ด๋ฉ๋๋ค.
<Welcome name="Sara" />
์๋ฆฌ๋จผํธ๋กReactDOM.render()
๋ฅผ ํธ์ถํฉ๋๋ค.- React๋
{name: 'Sara'}
๋ฅผ props๋ก ํ์ฌWelcome
์ปดํฌ๋ํธ๋ฅผ ํธ์ถํฉ๋๋ค. Welcome
์ปดํฌ๋ํธ๋ ๊ฒฐ๊ณผ์ ์ผ๋ก<h1>Hello, Sara</h1>
์๋ฆฌ๋จผํธ๋ฅผ ๋ฐํํฉ๋๋ค.- React DOM์
<h1>Hello, Sara</h1>
์๋ฆฌ๋จผํธ์ ์ผ์นํ๋๋ก DOM์ ํจ์จ์ ์ผ๋ก ์ ๋ฐ์ดํธํฉ๋๋ค.
์ฃผ์: ์ปดํฌ๋ํธ์ ์ด๋ฆ์ ํญ์ ๋๋ฌธ์๋ก ์์ํฉ๋๋ค.
React๋ ์๋ฌธ์๋ก ์์ํ๋ ์ปดํฌ๋ํธ๋ฅผ DOM ํ๊ทธ๋ก ์ฒ๋ฆฌํฉ๋๋ค. ์๋ฅผ ๋ค์ด
<div />
๋ HTML div ํ๊ทธ๋ฅผ ๋ํ๋ด์ง๋ง,<Welcome />
์ ์ปดํฌ๋ํธ๋ฅผ ๋ํ๋ด๋ฉฐ ๋ฒ์ ์์Welcome
์ด ์์ด์ผ ํฉ๋๋ค.์ด ๊ท์น์ ๋ํ ์์ธํ ๋ด์ฉ์ ์ฌ๊ธฐ์์ ํ์ธํ ์ ์์ต๋๋ค.
์ปดํฌ๋ํธ ํฉ์ฑ
์ปดํฌ๋ํธ๋ ์์ ์ ์ถ๋ ฅ์ ๋ค๋ฅธ ์ปดํฌ๋ํธ๋ฅผ ์ฐธ์กฐํ ์ ์์ต๋๋ค. ์ด๋ ๋ชจ๋ ์ธ๋ถ ๋จ๊ณ์์ ๋์ผํ ์ถ์ ์ปดํฌ๋ํธ๋ฅผ ์ฌ์ฉํ ์ ์์์ ์๋ฏธํฉ๋๋ค. React ์ฑ์์๋ ๋ฒํผ, ํผ, ๋ค์ด์ผ๋ก๊ทธ, ํ๋ฉด ๋ฑ์ ๋ชจ๋ ๊ฒ๋ค์ด ํํ ์ปดํฌ๋ํธ๋ก ํํ๋ฉ๋๋ค.
์๋ฅผ ๋ค์ด Welcome
์ ์ฌ๋ฌ ๋ฒ ๋ ๋๋งํ๋ App
์ปดํฌ๋ํธ๋ฅผ ๋ง๋ค ์ ์์ต๋๋ค.
function Welcome(props) {
return <h1>Hello, {props.name}</h1>;
}
function App() {
return (
<div>
<Welcome name="Sara" /> <Welcome name="Cahal" /> <Welcome name="Edite" /> </div>
);
}
ReactDOM.render(
<App />,
document.getElementById('root')
);
์ผ๋ฐ์ ์ผ๋ก ์ React ์ฑ์ ์ต์์์ ๋จ์ผ App
์ปดํฌ๋ํธ๋ฅผ ๊ฐ์ง๊ณ ์์ต๋๋ค. ํ์ง๋ง ๊ธฐ์กด ์ฑ์ React๋ฅผ ํตํฉํ๋ ๊ฒฝ์ฐ์๋ Button
๊ณผ ๊ฐ์ ์์ ์ปดํฌ๋ํธ๋ถํฐ ์์ํด์ ๋ทฐ ๊ณ์ธต์ ์๋จ์ผ๋ก ์ฌ๋ผ๊ฐ๋ฉด์ ์ ์ง์ ์ผ๋ก ์์
ํด์ผ ํ ์ ์์ต๋๋ค.
์ปดํฌ๋ํธ ์ถ์ถ
์ปดํฌ๋ํธ๋ฅผ ์ฌ๋ฌ ๊ฐ์ ์์ ์ปดํฌ๋ํธ๋ก ๋๋๋ ๊ฒ์ ๋๋ ค์ํ์ง ๋ง์ธ์.
๋ค์ Comment
์ปดํฌ๋ํธ๋ฅผ ์ดํด๋ด
์๋ค.
function Comment(props) {
return (
<div className="Comment">
<div className="UserInfo">
<img className="Avatar"
src={props.author.avatarUrl}
alt={props.author.name}
/>
<div className="UserInfo-name">
{props.author.name}
</div>
</div>
<div className="Comment-text">
{props.text}
</div>
<div className="Comment-date">
{formatDate(props.date)}
</div>
</div>
);
}
์ด ์ปดํฌ๋ํธ๋ author
(๊ฐ์ฒด), text
(๋ฌธ์์ด) ๋ฐ date
(๋ ์ง)๋ฅผ props๋ก ๋ฐ์ ํ ์์
๋ฏธ๋์ด ์น ์ฌ์ดํธ์ ์ฝ๋ฉํธ๋ฅผ ๋ํ๋
๋๋ค.
์ด ์ปดํฌ๋ํธ๋ ๊ตฌ์ฑ์์๋ค์ด ๋ชจ๋ ์ค์ฒฉ ๊ตฌ์กฐ๋ก ์ด๋ฃจ์ด์ ธ ์์ด์ ๋ณ๊ฒฝํ๊ธฐ ์ด๋ ค์ธ ์ ์์ผ๋ฉฐ, ๊ฐ ๊ตฌ์ฑ์์๋ฅผ ๊ฐ๋ณ์ ์ผ๋ก ์ฌ์ฌ์ฉํ๊ธฐ๋ ํ๋ญ๋๋ค. ์ด ์ปดํฌ๋ํธ์์ ๋ช ๊ฐ์ง ์ปดํฌ๋ํธ๋ฅผ ์ถ์ถํ๊ฒ ์ต๋๋ค.
๋จผ์ Avatar
๋ฅผ ์ถ์ถํ๊ฒ ์ต๋๋ค.
function Avatar(props) {
return (
<img className="Avatar" src={props.user.avatarUrl} alt={props.user.name} /> );
}
Avatar
๋ ์์ ์ด Comment
๋ด์์ ๋ ๋๋ง ๋๋ค๋ ๊ฒ์ ์ ํ์๊ฐ ์์ต๋๋ค. ๋ฐ๋ผ์ props์ ์ด๋ฆ์ author
์์ ๋์ฑ ์ผ๋ฐํ๋ user
๋ก ๋ณ๊ฒฝํ์์ต๋๋ค.
props์ ์ด๋ฆ์ ์ฌ์ฉ๋ context๊ฐ ์๋ ์ปดํฌ๋ํธ ์์ฒด์ ๊ด์ ์์ ์ง๋ ๊ฒ์ ๊ถ์ฅํฉ๋๋ค.
์ด์ Comment
๊ฐ ์ด์ง ๋จ์ํด์ก์ต๋๋ค.
function Comment(props) {
return (
<div className="Comment">
<div className="UserInfo">
<Avatar user={props.author} /> <div className="UserInfo-name">
{props.author.name}
</div>
</div>
<div className="Comment-text">
{props.text}
</div>
<div className="Comment-date">
{formatDate(props.date)}
</div>
</div>
);
}
๋ค์์ผ๋ก Avatar
์์ ์ฌ์ฉ์์ ์ด๋ฆ์ ๋ ๋๋งํ๋ UserInfo
์ปดํฌ๋ํธ๋ฅผ ์ถ์ถํ๊ฒ ์ต๋๋ค.
function UserInfo(props) {
return (
<div className="UserInfo"> <Avatar user={props.user} /> <div className="UserInfo-name"> {props.user.name} </div> </div> );
}
Comment
๊ฐ ๋์ฑ ๋จ์ํด์ก์ต๋๋ค.
function Comment(props) {
return (
<div className="Comment">
<UserInfo user={props.author} /> <div className="Comment-text">
{props.text}
</div>
<div className="Comment-date">
{formatDate(props.date)}
</div>
</div>
);
}
์ฒ์์๋ ์ปดํฌ๋ํธ๋ฅผ ์ถ์ถํ๋ ์์
์ด ์ง๋ฃจํด ๋ณด์ผ ์ ์์ต๋๋ค. ํ์ง๋ง ์ฌ์ฌ์ฉ ๊ฐ๋ฅํ ์ปดํฌ๋ํธ๋ฅผ ๋ง๋ค์ด ๋๋ ๊ฒ์ ๋ ํฐ ์ฑ์์ ์์
ํ ๋ ๋๊ฐ์ ๋ํ๋
๋๋ค. UI ์ผ๋ถ๊ฐ ์ฌ๋ฌ ๋ฒ ์ฌ์ฉ๋๊ฑฐ๋ (Button
, Panel
, Avatar
), UI ์ผ๋ถ๊ฐ ์์ฒด์ ์ผ๋ก ๋ณต์กํ (App
, FeedStory
, Comment
) ๊ฒฝ์ฐ์๋ ๋ณ๋์ ์ปดํฌ๋ํธ๋ก ๋ง๋๋ ๊ฒ ์ข์ต๋๋ค.
props๋ ์ฝ๊ธฐ ์ ์ฉ์ ๋๋ค.
ํจ์ ์ปดํฌ๋ํธ๋ ํด๋์ค ์ปดํฌ๋ํธ ๋ชจ๋ ์ปดํฌ๋ํธ์ ์์ฒด props๋ฅผ ์์ ํด์๋ ์ ๋ฉ๋๋ค. ๋ค์ sum
ํจ์๋ฅผ ์ดํด๋ด
์๋ค.
function sum(a, b) {
return a + b;
}
์ด๋ฐ ํจ์๋ค์ ์์ ํจ์๋ผ๊ณ ํธ์นญํฉ๋๋ค. ์ ๋ ฅ๊ฐ์ ๋ฐ๊พธ๋ ค ํ์ง ์๊ณ ํญ์ ๋์ผํ ์ ๋ ฅ๊ฐ์ ๋ํด ๋์ผํ ๊ฒฐ๊ณผ๋ฅผ ๋ฐํํ๊ธฐ ๋๋ฌธ์ ๋๋ค.
๋ฐ๋ฉด์ ๋ค์ ํจ์๋ ์์ ์ ์ ๋ ฅ๊ฐ์ ๋ณ๊ฒฝํ๊ธฐ ๋๋ฌธ์ ์์ ํจ์๊ฐ ์๋๋๋ค.
function withdraw(account, amount) {
account.total -= amount;
}
React๋ ๋งค์ฐ ์ ์ฐํ์ง๋ง ํ ๊ฐ์ง ์๊ฒฉํ ๊ท์น์ด ์์ต๋๋ค.
๋ชจ๋ React ์ปดํฌ๋ํธ๋ ์์ ์ props๋ฅผ ๋ค๋ฃฐ ๋ ๋ฐ๋์ ์์ ํจ์์ฒ๋ผ ๋์ํด์ผ ํฉ๋๋ค.
๋ฌผ๋ก ์ ํ๋ฆฌ์ผ์ด์ UI๋ ๋์ ์ด๋ฉฐ ์๊ฐ์ ๋ฐ๋ผ ๋ณํฉ๋๋ค. ๋ค์ ์ฅ์์๋ โstateโ๋ผ๋ ์๋ก์ด ๊ฐ๋ ์ ์๊ฐํฉ๋๋ค. React ์ปดํฌ๋ํธ๋ state๋ฅผ ํตํด ์ ๊ท์น์ ์๋ฐํ์ง ์๊ณ ์ฌ์ฉ์ ์ก์ , ๋คํธ์ํฌ ์๋ต ๋ฐ ๋ค๋ฅธ ์์์ ๋ํ ์๋ต์ผ๋ก ์๊ฐ์ ๋ฐ๋ผ ์์ ์ ์ถ๋ ฅ๊ฐ์ ๋ณ๊ฒฝํ ์ ์์ต๋๋ค.