Passing loop element to MenuItem onClick, the elements is always the last element in the array.
Passing loop element to MenuItem onClick should pass the current element being iterated over.
| Tech | Version |
|--------------|---------|
| Material-UI | beta23 |
| React | 16.2.0 |
| browser | chrome 62 |
| etc | macos |
Most helpful comment
@barrongineer , the code you posted in codesandbox has a problem:
Regardless of the button clicked, either
button oneorbutton two, both menus will be opened. The second menu will stay on top of the first one. Therefore, whenever you click the menu, it will console.logclosing two.If the code is updated to correctly open each menu when the corresponding button clicked, the problem will go.
I reckon you update your codesandbox example or this one should not be considered as a bug.
It's the code copied from your codesandbox, and both menus rely on the same state
open.````
class SimpleMenu extends React.Component {
state = {
anchorEl: null,
open: false,
things:['one', 'two']
};
handleClick = event => {
this.setState({ open: true, anchorEl: event.currentTarget });
};
handleRequestClose = (thing) => {
console.log(
closing ${thing})this.setState({ open: false });
};
render() {
return (
{this.state.things.map(thing => {
return (
aria-owns={this.state.open ? 'simple-menu' : null}
aria-haspopup="true"
onClick={this.handleClick}>
Open {thing}
}
}
````
All 2 comments
@barrongineer , the code you posted in codesandbox has a problem:
Regardless of the button clicked, either
button oneorbutton two, both menus will be opened. The second menu will stay on top of the first one. Therefore, whenever you click the menu, it will console.logclosing two.If the code is updated to correctly open each menu when the corresponding button clicked, the problem will go.
I reckon you update your codesandbox example or this one should not be considered as a bug.
It's the code copied from your codesandbox, and both menus rely on the same state
open.````
class SimpleMenu extends React.Component {
state = {
anchorEl: null,
open: false,
things:['one', 'two']
};
handleClick = event => {
this.setState({ open: true, anchorEl: event.currentTarget });
};
handleRequestClose = (thing) => {
console.log(
closing ${thing})this.setState({ open: false });
};
render() {
return (
{this.state.things.map(thing => {
return (
aria-owns={this.state.open ? 'simple-menu' : null}
aria-haspopup="true"
onClick={this.handleClick}>
Open {thing}
}
}
````
I see. I need to control the visibility with an expression rather than a boolean. Something like: