material-ui项目未显示。我想在SelectField中显示产品类别,但它不起作用。 当我点击选择字段时,它不会显示任何项目 我的组件的所有代码都在下面给出
import React, { Component } from 'react'
import RaisedButton from 'material-ui/RaisedButton';
import TextField from 'material-ui/TextField';
import SelectField from 'material-ui/SelectField';
import MenuItem from 'material-ui/MenuItem';
const style = {
padding: '10px',
textAlign: 'center'
};
const categories = ["mobile","laptop","camera"];
class AddProduct extends Component {
constructor(props) {
super();
this.state = {
productName: '',
productCategory: '',
}
this.submit = this.submit.bind(this);
this.inputHandler = this.inputHandler.bind(this);
}
inputHandler(e) {
this.setState({
[e.target.name]: e.target.value
})
}
handleChangeCategory = (event, index, value) => {this.setState({productCategory:value}); console.log("value", value)}
render() {
return (
<div ><center>
<h1>Add Product for Auction</h1>
<form onSubmit={this.submit} >
<br />
<br />
<SelectField
floatingLabelText="Catogory"
value={this.state.productCategory}
onChange={this.handleChangeCategory}
>
<MenuItem value={"mobile"} primaryText="Mobile" />
<MenuItem value={"laptop"} primaryText="Laptop" />
<MenuItem value={"camera"} primaryText="Camera" />
</SelectField><br />
<br />
<TextField
type="text"
hintText="Product Title"
name="productName"
value={this.state.productName}
floatingLabelText="Product Title"
onChange={this.inputHandler}
/>
<RaisedButton type="submit" label="Add Product" primary={false} secondary={true} /> <br /><br />
</form>
</center>
</div>
);
}
}
export default AddProduct;
答案 0 :(得分:3)
您尚未定义功能提交,并且根据您的代码,您正试图将其绑定。
this.submit = this.submit.bind(this);
定义函数提交
submit(){
// do something
}
尝试使用 ES6胖箭头函数,这样你就不必绑定每个函数,就像你在上面的代码中所做的那样。
尝试这样的事情(使用ES6胖箭头)
import React, { Component } from 'react'
import RaisedButton from 'material-ui/RaisedButton';
import TextField from 'material-ui/TextField';
import SelectField from 'material-ui/SelectField';
import MenuItem from 'material-ui/MenuItem';
const style = {
padding: '10px',
textAlign: 'center'
};
const categories = ["mobile","laptop","camera"];
class AddProduct extends Component {
constructor(props) {
super();
this.state = {
productName: '',
productCategory: '',
}
}
submit =()=>{
}
inputHandler =(e)=> {
this.setState({
[e.target.name]: e.target.value
})
}
handleChangeCategory = (event, index, value) => {this.setState({productCategory:value}); console.log("value", value)}
render() {
return (
<div ><center>
<h1>Add Product for Auction</h1>
<form onSubmit={this.submit} >
<br />
<br />
<SelectField
floatingLabelText="Catogory"
value={this.state.productCategory}
onChange={this.handleChangeCategory}
>
<MenuItem value={"mobile"} primaryText="Mobile" />
<MenuItem value={"laptop"} primaryText="Laptop" />
<MenuItem value={"camera"} primaryText="Camera" />
</SelectField><br />
<br />
<TextField
type="text"
hintText="Product Title"
name="productName"
value={this.state.productName}
floatingLabelText="Product Title"
onChange={this.inputHandler}
/>
<RaisedButton type="submit" label="Add Product" primary={false} secondary={true} /> <br /><br />
</form>
</center>
</div>
);
}
}
export default AddProduct;