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

react问题总结与归纳

时间:2016-04-13 02:06:58      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:

欢迎大家指导与讨论 : )

  【持续更新】本文主要记录笔者在学习中遇到的问题,并作出相应总结。有错误的地方希望各位能够支持。

  一、在es6中getInitialState( 摘要:  constructor(props)和this.state )

/*es6*/
class TodoList extends Component{
    constructor(props){
        super(props);
        this.state = {
            items: [‘hello‘, ‘world‘, ‘click‘, ‘me‘]
        }
    }
        render(){
        //..
       }        
}
/*es5*/
var TodoList = React.createClass({
      getInitialState: function() {
             return {items: [‘hello‘, ‘world‘, ‘click‘, ‘me‘]};
       },
       render(){
       //...
       }
}) 

  二、在es6中setState( 摘要: fun.bind(this)和this.setState )

/*es6*/
class TodoList extends Component{
    render(){
        return (
            <div>
                <button onClick={ this.handleAdd.bind(this) } >Add item</button>    
            </div>
        )
    }
    handleAdd(){
        var newItem = prompt(‘enter a new‘)
        var newItems = [ ...this.state.items, newItem ];
        this.setState({items: newItems})
    }
}

 

react问题总结与归纳

标签:

原文地址:http://www.cnblogs.com/BestMePeng/p/React_learn.html

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