【发布时间】:2019-11-19 07:55:40
【问题描述】:
我在我的一个 React 项目中使用 Material-UI 的 Select 组件。我需要分组显示下拉数据,因此我使用<MenuItem> 包裹<ListSubheader>。我很难获得MenuItems 的价值。如果我的代码有任何明显错误,请告诉我。
<FormControl>
<InputLabel>Product type</InputLabel>
<Select
id="product-type"
input={<Input id="grouped-select" />}
value={this.state.productType}
autoWidth={true}
style={{ width: 200 }}
onChange={(e, child) => {
console.log(e.target.value); // undefined!
}}
>
{this.state.productList.map((p, i) => {
const list = p[1];
let items = list.map((e, j) => {
return (
<MenuItem key={j} value={e.name}>
{e.name}
</MenuItem>
);
});
return (
<div>
<ListSubheader key={i}>{p[0]}</ListSubheader>
{items}
</div>
);
})}
</Select>
</FormControl>
【问题讨论】:
-
试试这个 onChange={handleChange} 函数是 const handleChange = event => { setAge(event.target.value); };
-
你能 console.log
this.state.productList告诉我们价值吗?您的数据也是来自 api 吗? -
@AtinSingh 是的,它来自 API,并且数据实际上就在那里。看起来像这样:` [ [“Garments”,[ {“pk”:2,“master_category”:{“pk”:1,“name”:“Garments”},“name”:“T恤”} ] ] ] `
-
@GauravRoy 我已经尝试创建一个handleChange 函数,但是,e.target.value 仍然未定义。
-
是数组还是数组?
标签: reactjs material-ui