reactjs - 试图选择一个 MenuItem,并让它显示在 Select,不显示,Material UI
问题描述
尝试在框中将第一个 MenuItem 作为默认选项的下拉菜单,直到您单击它,然后下拉菜单会出现并为您提供选择选项,例如 1、2、3,当您单击它时,选择出现,下拉菜单消失
class App extends Component<any, any> {
constructor(props) {
super(props);
this.state = {
rigName: '',
desc: '',
choice: '',
customAttribute: ''
}
}
handleSubmit = async (event: any) => {
event.preventDefault();
// const response = await
// this.props.onSubmit(response.data);
console.log(this.state)
// this.setState({rigName: ''})
}
handleChange(event) {
this.setState({choice: event.target.value})
}
render() {
return ( <FormControl variant="outlined" size="small" style={{ fontSize: 12 }}>
<InputLabel id="demo-customized-select-label">{this.state.choice}</InputLabel>
<Select
labelId="demo-customized-select-label"
id="demo-customized-select"
onChange={() => this.handleChange}
value={this.state.choice}
>
<MenuItem value="">
<em>None</em>
</MenuItem>
<MenuItem>ID</MenuItem>
<MenuItem>PSA</MenuItem>
<MenuItem>ExternalID</MenuItem>
</Select>
</FormControl>
);
}
}
export default App;
现在,下拉菜单有效,但是单击它后没有显示该选项,有什么想法吗?或者我错过了什么?
更新我已在 Select 内部更改为
onChange={this.handleChange.bind(this)}
现在我收到此错误
A component is changing a controlled SelectInput to be uncontrolled.
Elements should not switch from uncontrolled to controlled (or vice versa).
Decide between using a controlled or uncontrolled SelectInput element for the lifetime of the component.
解决方案
推荐阅读
- android - 如何在 Android 中获取并列出所有已安装/可用的导航应用程序及其图标?
- python - 如何使用 Python + 服务帐户创建 BigQuery 数据传输?
- python - 在 python 中,我想使用正则表达式在字符串中查找嵌入的项目列表
- javascript - Redux 状态中的更新将 React 组件的整个状态设置为其初始状态
- node.js - 如何在服务器端反应中实现 redux?
- php - 为 foreach() show.blade.php 问题提供的参数无效
- java - 如果语句返回空字符串的逻辑问题
- powershell - PowerShell - 将特定扩展移动到年/月/扩展文件夹?
- python - 有谁知道如何使用 yfinance 从 yahoo Finance 获取每周数据
- python - 预期条件等到可见 Selenium Python