使用ReactJS + Material-UI,我有一个名为
colors
的数组,其中包含不同颜色的字符串。例如,数组colors
具有3个颜色字符串:"white","blue","green"。然后,我想要在<DropDownMenu/>
(http://www.material-ui.com/#/components/dropdown-menu)内呈现每个颜色字符串作为<MenuItem/>
。当选择一个<MenuItem/>
时,我想控制台记录该特定颜色,例如选择“white”:console.log("white")。
因此,我使用了.forEach但是<DropDownMenu/>
没有显示任何字符串并且为空。我可能做错了什么?
这是代码:
constructor() {
super()
this.state = {
value: 1,
}
}
dropDownColorChange(event, index, value) {
this.setState({value: value})
//Not sure how to implement here dynamically based on array size. Would like to console.log the color string of the selected
}
render() {
var colors = ["white", "blue", "green"] //would be able to handle any array size
return (
<div>
<DropDownMenu
value={this.state.valueTwo}
onChange={this.dropDownColorChange}
>
{
<MenuItem value={1} primaryText="Select" />
colors.forEach(color => {
<MenuItem primaryText={color}/>
})
}
</DropDownMenu>
</div>
)
}
谢谢你
key
属性,这样 React 才能跟踪它们。 - Kafo