码迷,mamicode.com
首页 > 其他好文 > 详细

【react】子组件向父组件传值2

时间:2016-08-09 20:24:08      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:

 

var Grandson = React.createClass({
    handleSelect: function(event) {
        var s = event.target.value + "人";
        this.props.handleSelect(s);
    },
    render: function(){
        return (
            <div style={{border: "1px solid red",margin: "10px"}}>{this.props.name}:
                <select onChange={this.handleSelect}>
                    <option value="">男</option>
                    <option value="">女</option>
                </select>
            </div>
        )
    }
});
var Child = React.createClass({
    handleVal: function(event) {
        var v = event.target.value + "@china";
        this.props.handleVal(v);
    },
    render: function(){
        return (
            <div style={{border: "1px solid green",margin: "10px"}}>
                {this.props.name}:<input onChange={this.handleVal}/>
                <Grandson name="性别" handleSelect={this.props.handleSelect}/>
            </div>
        )
    }
});
var Parent = React.createClass({
    getInitialState: function(){
        return {
            username: ‘‘,
            sex: ‘‘
        }
    },
    handleVal: function(v){
        this.setState({username: v});
    },
    handleSelect: function(s) {
        this.setState({sex: s});
    },
    render: function(){
        return (
            <div style={{border: "1px solid #000",padding: "10px"}}>
                <div>用户姓名:{this.state.username}</div>
                <div>用户性别:{this.state.sex}</div>
                <Child name="姓名" handleVal={this.handleVal.bind(this)} handleSelect={this.handleSelect.bind(this)}/>
            </div>
        )
    }
});
React.render(
  <Parent />,
  document.getElementById(test)
);

 

【react】子组件向父组件传值2

标签:

原文地址:http://www.cnblogs.com/lixuemin/p/5754360.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!