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不幸的是,無狀態函數不支持引用 –