admin管理员组文章数量:1277324
I am trying to change state based on a dropdown selection using Material UI.
The issue is the code below (stripped down) returns the list item, so I do not know what to do next.
RETURNED from the console.log(event.target) in the handleClose method
<li tabindex="0" class="MuiButtonBase-root-23 MuiListItem-root-72 MuiListItem-default-75 MuiListItem-gutters-79 MuiListItem-button-80 MuiMenuItem-root-70" role="menuitem">
The stripped down ponent:
campaigns gets populated from an api call. Thanks for any thoughts.
import React, { Component } from "react";
import Button from "@material-ui/core/Button";
import Menu from "@material-ui/core/Menu";
import MenuItem from "@material-ui/core/MenuItem";
class ABC extends Component {
constructor(props) {
super(props);
this.state = {
campaigns: [],
anchorEl: null
};
this.handleClick = this.handleClick.bind(this);
this.handleClose = this.handleClose.bind(this);
}
// For the dropdown
handleClick = event => {
this.setState({ anchorEl: event.currentTarget });
};
handleClose = event => {
this.setState({ anchorEl: null });
console.log(event.target);
};
render() {
const { campaigns, anchorEl } = this.state;
return (
<div>
<Button
aria-owns={anchorEl ? "simple-menu" : null}
aria-haspopup="true"
onClick={this.handleClick}
>
CHOOSE CAMPAIGN
</Button>
<Menu
id="simple-menu"
anchorEl={anchorEl}
open={Boolean(anchorEl)}
onClose={this.handleClose}
>
{campaigns.map(item => (
<MenuItem key={item.Id} onClick={event => this.handleClose(event)}>
{item.Name}
</MenuItem>
))}
</Menu>
{JSON.stringify(this.state.value)}
</div>
);
}
}
I am trying to change state based on a dropdown selection using Material UI.
The issue is the code below (stripped down) returns the list item, so I do not know what to do next.
RETURNED from the console.log(event.target) in the handleClose method
<li tabindex="0" class="MuiButtonBase-root-23 MuiListItem-root-72 MuiListItem-default-75 MuiListItem-gutters-79 MuiListItem-button-80 MuiMenuItem-root-70" role="menuitem">
The stripped down ponent:
campaigns gets populated from an api call. Thanks for any thoughts.
import React, { Component } from "react";
import Button from "@material-ui/core/Button";
import Menu from "@material-ui/core/Menu";
import MenuItem from "@material-ui/core/MenuItem";
class ABC extends Component {
constructor(props) {
super(props);
this.state = {
campaigns: [],
anchorEl: null
};
this.handleClick = this.handleClick.bind(this);
this.handleClose = this.handleClose.bind(this);
}
// For the dropdown
handleClick = event => {
this.setState({ anchorEl: event.currentTarget });
};
handleClose = event => {
this.setState({ anchorEl: null });
console.log(event.target);
};
render() {
const { campaigns, anchorEl } = this.state;
return (
<div>
<Button
aria-owns={anchorEl ? "simple-menu" : null}
aria-haspopup="true"
onClick={this.handleClick}
>
CHOOSE CAMPAIGN
</Button>
<Menu
id="simple-menu"
anchorEl={anchorEl}
open={Boolean(anchorEl)}
onClose={this.handleClose}
>
{campaigns.map(item => (
<MenuItem key={item.Id} onClick={event => this.handleClose(event)}>
{item.Name}
</MenuItem>
))}
</Menu>
{JSON.stringify(this.state.value)}
</div>
);
}
}
Share
Improve this question
edited Jul 10, 2018 at 21:31
Tholle
113k22 gold badges208 silver badges197 bronze badges
asked Jul 10, 2018 at 21:26
ivan7707ivan7707
1,1561 gold badge13 silver badges25 bronze badges
1 Answer
Reset to default 9Update MenuItem as :
<MenuItem key={item.Id} onClick={(event) => this.handleClose(item, event)}>{item.Name}</MenuItem>
This way you'll get that particular item as the event handler's parameter. Also I notice that the same function is passed as the onClose handler for Menu ponent which looked confusing. That might cause issue if Menu fires onClose event with a different parameter.
本文标签: javascriptsetState from a React material UI MenuItem click eventStack Overflow
版权声明:本文标题:javascript - setState from a React material UI MenuItem click event - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741292489a2370629.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论