props
- μ¬μ©μκ° μ»΄ν¬λνΈλ₯Ό μ¬μ©νλ μ
μ₯μμ μ€μ
state
- propsμ κ°μ λ°λΌ λ΄λΆ ꡬνμ νμν κ°
π stateλ λ€μν λμμ νλ μ»΄ν¬λνΈ μ μμ νμ
μΈλΆμμ μ νμκ° μλ μ 보λ₯Ό μ¨κΈΈ λ μ¬μ©
// App.js
class App extends Component {
constructor(props){ // renderλ³΄λ€ λ¨Όμ μ€νλμ΄ μ»΄ν¬λνΈ μ΄κΈ°ν
super(props);
this.state = {
subject:{title: 'WEB', sub: 'World Wide Web!'}
}
}
render() {
return (
<div className="App">
// state μ¬μ©
<Subject
title={this.state.subject.title}
sub={this.state.subject.sub}>
</Subject>
</div>
);
}
}
App
μ κ°μ νμ μ»΄ν¬λνΈμΈ Subject
μ state κ°μ ν΅ν΄ μ λ¬ κ°λ₯// App.js
class App extends Component {
constructor(props){
super(props);
this.state = {
subject: {title: 'WEB', sub: 'World Wide Web!'},
contents: [
{id:1, title:'HTML', desc:'HTML is for information'},
{id:2, title:'CSS', desc:'CSS is for design'},
{id:3, title:'JavaScript', desc:'JavaScript is for interactive'}
]
}
}
render() {
return (
<div className="App">
...
<TOC data={this.state.contents}></TOC>
</div>
);
}
}
// TOC.js
class TOC extends Component {
render(){
var lists = [];
var data = this.props.data;
var i = 0;
while(i < data.length){
lists.push(<li><a href={"/content/"+data[i].id}>{data[i].title}</a></li>);
i = i + 1;
}
return (
<nav>
<ul>
{lists}
</ul>
</nav>
);
}
}
key
λΆμ¬lists.push(<li key={data[i].id}><a href={"/content/"+data[i].id}>{data[i].title}</a></li>);