react事件绑定

慕课网http://www.imooc.com/video/9821 学习

by Bruce Wang

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.3.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.3.0/react-dom.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.23/browser.min.js"></script>
<div id="container">
  
</div>

Babel + JSX

var TestClickComponent = React.createClass({
	handleClick:function(event){
		var tipE = ReactDOM.findDOMNode(this.refs.tip);

		if(tipE.style.display == 'none'){
			tipE.style.display = "inline";
		}else{
			tipE.style.display = "none";
		}

		event.stopPropagation();
		event.preventDefault();

	},
	render: function(){
			return (
<div>
	<button onClick= {this.handleClick} >显示|隐藏</button>
	<span ref='tip'>点击测试</span>
</div>
)
		}
});

var TestInputComponent = React.createClass({
	getInitialState: function(){
		return {
			inputContent: ''
		}
	},
	changeHandle: function(event){
		this.setState({
			inputContent: event.target.value
		});

		event.stopPropagation();
		event.preventDefault();
	},
	render: function(){
		return (
<div>
	<input type="text" onChange = {this.changeHandle}/>		
	<span>{this.state.inputContent}</span>
</div>
)
	}
});

ReactDOM.render(
<div>
	<TestClickComponent />		
	<TestInputComponent />		
</div>
,document.getElementById('container'))