React - ํฉ์ฑ
2022. 5. 7. 17:29ใReact
React - ํฉ์ฑ(React ๊ณต์๋ฌธ์)
https://ko.reactjs.org/docs/composition-vs-inheritance.html
์ด๋ค ์ปดํฌ๋ํธ๋ค์ ์ด๋ค ์์ ์๋ฆฌ๋จผํธ๊ฐ ๋ค์ด์ฌ ์ง ๋ฏธ๋ฆฌ ์์ํ ์ ์๋ ๊ฒฝ์ฐ๊ฐ ์๋ค. ๋ฒ์ฉ์ ์ธ '๋ฐ์ค' ์ญํ ์ ํ๋ Sidebar ํน์ Dialog์ ๊ฐ์ ์ปดํฌ๋ํธ์์ ์์ฃผ ๋ณผ ์ ์๋ค.
function FancyBorder(props) {
return (
<div className={'FancyBorder FancyBorder-' + props.color}>
{props.children}
</div>
);
}
function WelcomeDialog() {
return (
<FancyBorder color="blue">
<h1 className="Dialog-title">
Welcome
</h1>
<p className="Dialog-message">
Thank you for visiting our spacecraft!
</p>
</FancyBorder>
);
}
ํน์ํ
๋๋ก๋ ์ด๋ค ์ปดํฌ๋ํธ์ "ํน์ํ ๊ฒฝ์ฐ"์ธ ์ปดํฌ๋ํธ๋ฅผ ๊ณ ๋ คํด์ผ ํ ๊ฒฝ์ฐ๊ฐ ์๋ค. ๊ณต์๋ฌธ์์ ์์์๋ SignUpDialog, Dialog๋ฅผ ์์๋ก ๋ค๊ณ ์๋ค.
function FancyBorder(props) { //border๋ฅผ ์ถ๊ฐํ๋ ์ปดํฌ๋ํธ ํฉ์ฑ
return (
<div className={'FancyBorder FancyBorder-' + props.color}>
{props.children}
</div>
);
}
function Dialog(props) { //์ผ๋ฐ์ ์ธ ์ปดํฌ๋ํธ ํฉ์ฑ
return (
<FancyBorder color="blue">
<h1 className="Dialog-title">
{props.title} //title prop์ ๋ฐ์.
</h1>
<p className="Dialog-message">
{props.message} //message prop์ ๋ฐ์.
</p>
{props.children} //Dialog์๋์ ์์ ์๋ฆฌ๋จผํธ๋ค์ ์ถ๋ ฅ
</FancyBorder>
);
}
class SignUpDialog extends React.Component {
constructor(props) {
super(props);
this.handleChange = this.handleChange.bind(this);
this.handleSignUp = this.handleSignUp.bind(this);
this.state = {login: ''};
}
render() {
return (
<Dialog title="Mars Exploration Program" //์ข ๋ ๊ตฌ์ฒด์ ์ธ ์ปดํฌ๋ํธ SignUpDialog์์ ์ผ๋ฐ์ ์ธ ์ปดํฌ๋ํธ ๋ ๋๋ง
message="How should we refer to you?">
<input value={this.state.login}
onChange={this.handleChange} />
<button onClick={this.handleSignUp}>
Sign Me Up!
</button>
</Dialog>
);
}
'React' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
React - useRef (0) | 2022.05.07 |
---|---|
State ๋์ด์ฌ๋ฆฌ๊ธฐ (0) | 2022.05.07 |
React - useEffect, useCallback (0) | 2022.04.11 |