undefined

react属性与事件之事件与属性的双向绑定

bodyindex.js

s

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
import React from 'react';
import BodyChild from './bodychild';
export default class BodyIndex extends React.Component {
constructor() {
super(); //调用基类的所有的初始化方法
this.state = {
username: "Parry",
age: 20
}; //初始化赋值
};
changeUserInfo(age) {
this.setState({age: age});
};
handleChildValueChange(event) {
this.setState({age: event.target.value});
};
render() {
// setTimeout(()=>{
// //更改 state 的时候
// this.setState({username: "IMOOC",age : 30});
// },4000);
return (
<div>
<h2>页面的主体内容</h2>
<p>{this.props.userid} {this.props.username}</p>
<p>age: {this.state.age}</p>
<input type="button" value="提交" onClick={this.changeUserInfo.bind(this,99)}/>
<BodyChild handleChildValueChange={this.handleChildValueChange.bind(this)}/>
</div>
)
}
}

bodychild.js

1
2
3
4
5
6
7
8
9
10
11
12
13
import React from 'React';

export default class BodyChild extends React.Component{

render(){
return(
<div>
<p>子页面输入:<input type="text" onChange={this.props.handleChildValueChange}/></p>
</div>
)
}

}

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
var React = require('react');
var ReactDOM = require('react-dom');
import ComponentHeader from './components/header';
import ComponentFooter from './components/footer';
import BodyIndex from './components/bodyindex';
class Index extends React.Component {

componentWillMount(){
//定义你的逻辑即可
console.log("Index - componentWillMount");
}

componentDidMount(){
console.log("Index - componentDidMount");
}

render() {
/*
var component;
if (用户已登录) {
component = <ComponentLoginedHeader/>
}
else{
component = <ComponentHeader/>
}
*/
return (
<div>
<ComponentHeader/>
<BodyIndex userid={123456} username={"nick"}/>
<ComponentFooter/>
</div>
);
}
}
ReactDOM.render(
<Index/>, document.getElementById('example'));
觉得本站不错,请作者吃根辣条