【发布时间】:2019-12-30 06:01:16
【问题描述】:
我正在尝试使用 React Hooks 和 Material UI 创建一个简单的选择标签,但不知道如何设置菜单中所选项目的值。
我设法从 hooks 数组中获取菜单项,但一旦我选择一个,代码就会中断。
const [values, setValues] = React.useState([
"Bam",
"Kate",
"Nicole",
"Aaron"
]);
function handleChange(event) {
setValues(oldValues => ({
...oldValues,
[event.target.value]: event.target.value
}));
}
<FormControl className={classes.formControl}>
<InputLabel htmlFor="agent-simple">Agent</InputLabel>
<Select
value={values.value}
onChange={handleChange}
inputProps={{
name: "agent",
id: "age-simple"
}}
>
{values.map((value, index) => {
return <MenuItem value={value}>{value}</MenuItem>;
})}
</Select>
</FormControl>
我收到此错误TypeError: values.map is not a function
感谢您的帮助。
【问题讨论】:
-
嗨,bamerf,刚刚给你写了一个解决方案,如果有帮助,请告诉我并检查沙箱 :)
标签: reactjs material-ui react-hooks