๐ src > App.js
import logo from './logo.svg';
import './App.css';
function Article(props) {
return <article>
<h2>{props.title}</h2>
{props.body}
</article>
}
function Header(props) {
return <header>
<h1><a href="/" onClick={function(event){
event.preventDefault();
props.onChangeMode()
}}>{props.title}</a></h1>
</header>
}
function Nav(props) {
const lis = []
for(let i = 0; i < props.topics.length; i++) {
let t = props.topics[i];
lis.push(<li key={t.id}>
<a id={t.id} href={'/read/'+t.id} onClick={event => {
event.preventDefault()
props.onChangeMode(event.target.id)
}}>{t.title}</a>
</li>)
}
return <nav>
<ol>
{lis}
</ol>
</nav>
}
function App() {
const topics = [
{id:1, title:'html', body:'html is ...'},
{id:2, title:'css', body:'css is ...'},
{id:3, title:'javascript', body:'javascript is ...'}
]
return (
<div>
<Header title="REACT" onChangeMode={function(){
alert('header')
}}></Header>
<Nav topics={topics} onChangeMode={(id) =>
alert(id)}></Nav>
<Article title="Welcom" body="Hello, WEB"></Article>
</div>
);
}
export default App;
prop
: componunt๋ฅผ ์ฌ์ฉํ๋ ์ธ๋ถ์๋ฅผ ์ํ ๋ฐ์ดํฐstate
: componunt๋ฅผ ๋ง๋๋ ๋ด๋ถ์๋ฅผ ์ํ ๋ฐ์ดํฐ๐ src > App.js
1. const mode = 'WELCOM';
์ถ๊ฐ
mode์ ๊ฐ์ด ๋ฌด์์ด๋์ ๋ฐ๋ผ์ ๋ณธ๋ฌธ์ ๋ด์ฉ์ด ๋ฌ๋ผ์ง๊ฒ ํ๊ณ ์ถ๋ค.
function App() {
const mode = 'WELCOM';
const topics = [
{id:1, title:'html', body:'html is ...'},
{id:2, title:'css', body:'css is ...'},
{id:3, title:'javascript', body:'javascript is ...'}
]
let content = null;
if(mode === 'WELCOME') {
content = <Article title="Welcom" body="Hello, WEB"></Article>
} else if (mode === 'READ') {
content = <Article title="READ" body="Hello, READ"></Article>
}
return (
<div>
<Header title="REACT" onChangeMode={function(){
alert('header')
}}></Header>
<Nav topics={topics} onChangeMode={(id) =>
alert(id)}></Nav>
{content}
</div>
);
}
mode
๊ฐ์ event๊ฐ ๋ฐ์ํ์ ๋ ๋ณ๊ฒฝํด์ฃผ๋๋ก ์์ ํ ๊ฒ