[英]How do I show which item in the Menu List is selected with Material UI React?
I am trying to dynamically style the menu list items based on setting the selected
prop to true or false. 我正在尝试根据将selected
属性设置为true或false来动态设置菜单列表项的样式。
I am trying to use the onClick
method and to read the event.target.name
, change the state of the value
provided by each menu item and then finally use the selected
prop to check if the value is true or false. 我正在尝试使用onClick
方法并读取event.target.name
,更改每个菜单项提供的value
的状态,然后最终使用selected
prop来检查该值是true还是false。 For some reason, it is not picking up the event.taget.name
that I am trying to log. 由于某些原因,它没有选择我尝试登录的event.taget.name
。
Above render: 上方渲染:
constructor(props) {
super(props);
this.state = {
notFound: false,
value: false,
anchorEl: null,
industry: ''
};
}
handleIndustriesSelect = event => {
this.setState({ [event.target.name]: event.target.value });
console.log('target',event.target.name)
this.handleIndustriesClose()
}
Below render 下方渲染
<Menu
id="industries-menu"
anchorEl={anchorEl}
open={Boolean(anchorEl)}
onClose={this.handleIndustriesClose}
MenuListProps={{
name: 'industry'
}}
>
<MenuItem value={'aerospace'} selected={this.state.industry === 'aerospace'} onClick={this.handleIndustriesSelect} component={Link} to='/home/industry/aerospace'>Aerospace</MenuItem>
<MenuItem onClick={this.handleIndustriesSelect} component={Link} to='/home/industry/automotive'>Automotive</MenuItem>
<MenuItem onClick={this.handleIndustriesSelect} component={Link} to='/home/industry/energy'>Energy</MenuItem>
<MenuItem onClick={this.handleIndustriesSelect} component={Link} to='/home/industry/industrial'>Industrial</MenuItem>
<MenuItem onClick={this.handleIndustriesSelect} component={Link} to='/home/industry/marine'>Marine</MenuItem>
<MenuItem onClick={this.handleIndustriesSelect} component={Link} to='/home/industry/medical-technologies'>Medical Technologies</MenuItem>
<MenuItem onClick={this.handleIndustriesSelect} component={Link} to='/home/industry/tool-manufacturers'>Tool Manufacturers</MenuItem>
<MenuItem onClick={this.handleIndustriesSelect} component={Link} to='/home/industry/mixed-parts'>Mixed Parts</MenuItem>
<MenuItem onClick={this.handleIndustriesSelect} component={Link} to='/home/industry/watch'>Watch</MenuItem>
</Menu>
You can not use state
to select the menu item because the states are not kept when you click the menu items every time. 您不能使用state
来选择菜单项,因为每次单击菜单项时都不会保留状态。 When you click the menu item, it is supposed to change the url and the component will be re-rendered. 当您单击菜单项时,应该更改url,组件将重新呈现。
So the solution is to check if the current location(url) is matched with the menu item's target url. 因此解决方案是检查当前位置(URL)是否与菜单项的目标URL相匹配。 If matched, the selected
prop of menu item should be true. 如果匹配,则菜单项的selected
属性应为true。 In order to get the current location as a prop in the component, you will need to use withRouter
HOC of react-router
. 为了获得当前位置作为组件中的道具,您将需要使用react-router
withRouter
HOC。
Here is a full code that verify how this works. 这是验证其工作原理的完整代码。 https://codesandbox.io/s/j432ox8255 https://codesandbox.io/s/j432ox8255
import React from 'react'; import { Link} from 'react-router-dom'; import { withRouter } from 'react-router' import Button from '@material-ui/core/Button'; import Menu from '@material-ui/core/Menu'; import MenuItem from '@material-ui/core/MenuItem'; class SimpleMenu extends React.Component { state = { notFound: false, value: false, anchorEl: null, }; handleIndustriesOpen = event => { this.setState({ anchorEl: event.currentTarget }); }; handleIndustriesClose = () => { this.setState({ anchorEl: null }); }; render() { const { location: { pathname } } = this.props; const { anchorEl } = this.state; return ( <div> <Button aria-owns={anchorEl ? 'industries-menu' : null} aria-haspopup="true" onClick={this.handleIndustriesOpen} > Open Menu </Button> <Menu id="industries-menu" anchorEl={anchorEl} open={Boolean(anchorEl)} onClose={this.handleIndustriesClose} MenuListProps={{ name: 'industry' }} > <MenuItem selected={pathname === '/home/industry/aerospace'} onClick={this.handleIndustriesSelect} component={Link} to='/home/industry/aerospace'>Aerospace</MenuItem> <MenuItem selected={pathname === '/home/industry/automotive'} onClick={this.handleIndustriesClose} component={Link} to='/home/industry/automotive'>Automotive</MenuItem> <MenuItem selected={pathname === '/home/industry/energy'} onClick={this.handleIndustriesClose} component={Link} to='/home/industry/energy'>Energy</MenuItem> <MenuItem selected={pathname === '/home/industry/industrial'} onClick={this.handleIndustriesClose} component={Link} to='/home/industry/industrial'>Industrial</MenuItem> <MenuItem selected={pathname === '/home/industry/marine'} onClick={this.handleIndustriesClose} component={Link} to='/home/industry/marine'>Marine</MenuItem> <MenuItem selected={pathname === '/home/industry/medical-technologies'} onClick={this.handleIndustriesClose} component={Link} to='/home/industry/medical-technologies'>Medical Technologies</MenuItem> <MenuItem selected={pathname === '/home/industry/tool-manufacturers'} onClick={this.handleIndustriesClose} component={Link} to='/home/industry/tool-manufacturers'>Tool Manufacturers</MenuItem> <MenuItem selected={pathname === '/home/industry/mixed-parts'} onClick={this.handleIndustriesClose} component={Link} to='/home/industry/mixed-parts'>Mixed Parts</MenuItem> <MenuItem selected={pathname === '/home/industry/watch'} onClick={this.handleIndustriesClose} component={Link} to='/home/industry/watch'>Watch</MenuItem> </Menu> </div> ); } } export default withRouter(SimpleMenu);
You need to set name
explicitly on each MenuItem
. 您需要在每个MenuItem
上显式设置name
。
MenuListProps
does apply to inner MenuList
component only, not to MenuItem
s. MenuListProps
确实仅适用于内部MenuList
组件,不适用于MenuItem
。
But, in your case I'd not use name prop, just set it in setState
directly: 但是,在您的情况下,我不使用名称prop,只需在setState
直接设置它即可:
handleIndustriesSelect = event => {
this.setState({ industry: event.target.value });
Also, it looks like e.target.value
is also undefined
, if so is true for you, then you have to pass value
prop too, which is a sign of overcomplication. 另外,看起来e.target.value
也是undefined
,如果对您来说确实如此,那么您也必须传递value
属性,这是过度复杂的标志。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.