In my ReactJS + Material-UI project, I am working with an array named colors
that contains different color strings such as "white", "blue", and "green. My goal is to render each color string as a <MenuItem/>
within a <DropDownMenu/>
component (http://www.material-ui.com/#/components/dropdown-menu). When a user selects a color from the dropdown menu, I want to log that specific color to the console (e.g., if "white" is chosen, then console.log("white")).
I tried using .forEach method but the dropdown menu does not display any strings and remains empty. What could be causing this issue?
Here is an excerpt of the code:
constructor() {
super()
this.state = {
value: 1,
}
}
dropDownColorChange(event, index, value) {
this.setState({value: value})
//I need help on dynamically implementing this section based on array size. The goal is to console.log the color string of the selected item
}
render() {
var colors = ["white", "blue", "green"]; //able to accommodate arrays of any size
return (
<div>
<DropDownMenu
value={this.state.valueTwo}
onChange={this.dropDownColorChange}
>
{
<MenuItem value={1} primaryText="Select" />
colors.forEach(color => {
<MenuItem primaryText={color}/>
})
}
</DropDownMenu>
</div>
)
}
Thank you for your assistance.