-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
50 lines (46 loc) · 1.16 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import MyReact from './MyReact'
import MyReactDOM from './MyReactDom'
//得到了 Component 中的 props,render方法
// new App 时,就会去渲染组件
class App extends MyReact.Component {
constructor(props) {
super(props)
this.state = {
name: '张三',
job: '后端工程师',
hobby: '看电影'
}
}
render() {
return (
<div className="wrapper">
<h1 className="title">hello <span>{ this.state.name }</span></h1>
<p>hobby: { this.state.hobby }</p>
<Job job={ this.state.job } onModifyJob = { this.onModifyJob.bind(this) }></Job>
<Hobby hobby={ this.state.hobby }></Hobby>
</div>
)
}
onModifyJob(newJob) {
this.setState({job: newJob})
}
}
class Job extends MyReact.Component {
render() {
return (
<div className="job">
我的工作是{ this.props.job }
<button onClick = { this.modifyJob.bind(this) }>修改工作</button>
</div>
)
}
modifyJob() {
this.props.onModifyJob('React工程师')
}
}
function Hobby(props) {
return (
<p>我的兴趣是{ props.hobby }</p>
)
}
MyReactDOM.render(<App/>, document.querySelector('#app'))