setState函数使用方法

在组件完成加载之后,我们可以通过setState方法来改变我们设置的属性的值。

首先在render函数中加入一个input元素,用于触发onChange事件,然后调用setState方法。

/*** @jsx React.DOM */
var APP = React.createClass({
getInitialState: function() {
return {
txt: 'this is some text from initial state'设置默认值 这里的默认值必须冒号不能是等于,多个并调用逗号隔开
}
},
updateTxt: function(e) {
this.setState({txt: e.target.value})
}, 通过调用事件响应来设置值
render: function(){
return (
<div>
<input type="text" onChange={this.updateTxt} />调用上面的函数
<p>{this.state.txt}</p>
</div>
)
}
});

posted @ 2018-01-05 10:29  艾礼富  阅读(1348)  评论(0编辑  收藏  举报