React 事件 监听

实例

  var TestComponent= React.createClass({
         getInitialState:function(){
            return {contentValue:''}
         },
         ContentHandler:function(event){
             console.log(event);
             console.log(event.target.value)
             this.setState({contentValue:event.target.value})
         },
         render:function(){
            return (
                <div>
                    <ul>
                        <input type="text" ref="textInput"  onChange={this.ContentHandler} />
                        <li>{this.state.contentValue}</li>
                        <li>3</li>
                    </ul>
                </div>
            )
         }
    })
    var Comp=React.createClass({
        getInitialState:function(){
            return {
                divStyle:{
                     fontSize:'12px',
                    borderColor:'#0c9',
                    borderStyle:'solid',
                    borderWidth:'2px'
                },
                count:0,

            }
        },
        render:function(){
           return <div ref="head" style={this.state.divStyle}  className="wm-h">{this.props.name} <h1>我就试试{this.props.title}</h1>

                    <button onClick={this.handleClick}>{this.state.count}</button>
           </div>;
        },
        handleClick:function(event){
            var d=this.refs.head;
            console.log(this.refs.head)
            d.style.background="#ccc";


            this.setState(function(state){
                return {count:state.count+1}
            })
        }

    });
       ReactDOM.render(<div>
           <Comp name="666" title="44944"/>
        <TestComponent></TestComponent>
       </div>
        ,
        document.getElementById('app')

        );

同过 on + 驼峰事件名( 例如 onClick) 来添加时间绑定

<button onClick={this.handleClick}>{this.state.count}</button>

React .获取render component 的组件的dom节点:

使用 ref="nodeName";
以  this.refs.nodeName 获取实例的DOM 节点

input 的数据获取

  <input type="text" ref="textInput"  onChange={this.ContentHandler} />

input的数据获取

 ContentHandler:function(event){
             this.setState({contentValue:event.target.value})
         }

监听change 事件 将数据绑定到 contentValue 上

组件的数据传递

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值