0
我在下面的腳本:http://codepen.io/AlexanderWeb00/pen/gLVbjL?editors=0010作出反應得到表單輸入值提交,並顯示它
class Wrapper extends React.Component {
constructor(props) {
super(props);
this.state = {
name: ''
};
}
handleFirstName(e) {
this.setState({name: e.target.value});
}
render(){
return (
<section>
<h2>welcome</h2>
<TShirt name={this.state.name} />
<FormButton action={this.handleFirstName.bind(this)} />
</section>
)
}
};
class TShirt extends React.Component {
render(){
return (
<section>
<div>
<p>Name: {this.props.name}</p>
</div>
</section>
)
}
};
class Extention extends React.Component {
render(){
return (
<section>
<div>
<p>badaboom</p>
</div>
</section>
)
}
};
var FormButton = React.createClass({
getInitialState: function() {
return {'submitted': false};
},
handleSubmit: function(e) {
e.preventDefault();
this.setState({'submitted': true });
},
render: function() {
if (this.state.submitted) {
return <Extention/>;
}
else {
return (
<form role="form" onSubmit={this.handleSubmit}>
<input type="text" />
<input type="submit" value="submit" />
</form>
);
}
}
});
ReactDOM.render(<Wrapper />, document.getElementById('app'));
我想要顯示從輸入
<input type="text" />
這裏收集的值:
<p>Name: {this.props.name}</p>, inside the TShirt component.
此刻只顯示一個新的c表單提交後的omponent。在構建應用程序時,Tshirt組件將更新更多的內容。