React

简介

  1. 前段UI的开发框架
  2. 前置内容
  3. 学习地址

hello world

<div id="container">
    <!-- This element's contents will be replaced with your component. -->
</div>
var Hello = React.createClass({
  render: function() {
    return <div>Hello {this.props.name}</div>;
    //这里的节点不是dom节点
    //这里的{ }指的是要取js的表达式的值
  }
});

ReactDOM.render(
  <Hello name="World" />,
  document.getElementById('container')
);

JSX和Style

JSX
style
    
return <div className='alert-text'></div>
    
//在jsx中style需要用样式对象表示.在jsx中js的表达式需要用{}
//样式key使用的驼峰写法.
var style = {color:'red'}
return <div style={style}></div>

React-Components-Lifecycle

component-lifecycle

hook

Mounting

Updating

propsstate

react事件绑定

var Hello  = React.createClass({
    handleClick:function(event){
        var tip = this.refs.tip//拿到的是react组件
        var tipEl = React.findDOMNode(this.refs.tip);//拿到真实的dom
        event.stopPropagation();
        event.preventDefault();
    },
    render:function(){
        return (
            <div>
                <button onClick={this.handleClick}>
                <span ref='tip'>
                </button>
            </div>
        );
    }
});
var Hello = React.createClass({
    handleClick:function(event){
        this.setState({
            inputContent:event.target.value
        });
        event.stopPropagation();
        event.preventDefault();
    },
    render:function(){
        return (
                <div>
                    <input type='text' onChange={this.handleChange}/>
                    <span>{this.state.inputContent}</span>
                </div>
    }
});

基础知识