0
我以React开头,我试图创建一个简单的表单,说你好名称!这是“React.js”这样做的方式吗?
但是我觉得有2个状态元素不是正确的方法来做到这一点。
有谁知道或相信有更好的方法来做到这一点?顺便说一下,我知道我可以将名称状态绑定到h2,但我希望它在点击时发生。
var Name = React.createClass({
getInitialState:function(){
return {
inputname:'',
h2name:''
};
},
showName:function(event){
event.preventDefault();
this.setState({h2name:this.state.inputname});
},
updateName:function(event){
this.setState({inputname:event.target.value});
}
,
render:function(){
return (
<div>
<form onSubmit={this.showName}>
<input onChange={this.updateName} placeholder="Enter your name"></input>
<button type="submit">Show</button>
</form>
<h2>Hello {this.state.h2name}!</h2>
</div>
);
}
});
ReactDOM.render(<Name />,document.getElementById('mount-point'));
非常感谢您的文档:) – Leordas