1
有人可以告诉我关于如何使用无状态组件构建表单的示例代码/说明吗?使用无状态组件创建注册表单?
我也无法找到一个使用refs的Material UI窗体示例。
注意:我正在使用Material UI组件。
这是我目前的创建形式:
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
name: '',
school: '',
};
}
/*in order to access state from within function, I had to bind this when I made
the call to submitCandidate i.e. onClick={this.submitCandidate.bind(this)}
*/
submitCandidate(event){
event.preventDefault();
//assign the form fields to candidates object
var newCandidate = {
name: this.state.name,
school: this.state.school,
date: this.state.date
}
//inserts candidates object to database
Candidates.insert(newCandidate);
render() {
return (
<MuiThemeProvider muiTheme={getMuiTheme(lightBaseTheme)}>
<div>
<TextField
hintText={"Enter Your Name"}
floatingLabelText={"Name"}
value={this.state.name}
onChange={e => this.setState({ name: e.target.value })}
/>
<TextField
hintText={"Enter Your School"}
floatingLabelText={"School"}
value={this.state.school}
onChange={e => this.setState({ school: e.target.value })}
/>
<RaisedButton type="submit" label="Submit" onClick={this.submitCandidate.bind(this)} />
</div>
</MuiThemeProvider>
);
}
}
而不是依赖'onChange'处理程序,您可以在您的输入中使用'ref',并在提交时立即收集所有值 - https://facebook.github.io/react/docs/more- about-refs.html – Deryck
@Deryck不幸的是,无状态函数不支持引用 –