欢迎大家指导与讨论 : )
【持续更新】本文主要记录笔者在学习中遇到的问题,并作出相应总结。有错误的地方希望各位能够支持。
一、在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})
}
}
原文:http://www.cnblogs.com/BestMePeng/p/React_learn.html