Col*_*iel 1 javascript reactjs material-ui
我正在尝试根据将selected
prop 设置为true或false 来动态设置菜单列表项的样式。
我试图使用该onClick
方法并读取event.target.name
,更改value
每个菜单项提供的状态,然后最终使用selected
prop检查该值是true还是false。由于某种原因,它没有获取event.taget.name
我要记录的日志。
上方渲染:
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()
}
Run Code Online (Sandbox Code Playgroud)
下方渲染
<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>
Run Code Online (Sandbox Code Playgroud)
您不能用于state
选择菜单项,因为每次单击菜单项时都不会保留状态。当您单击菜单项时,应该更改url,组件将重新呈现。
因此解决方案是检查当前位置(URL)是否与菜单项的目标URL相匹配。如果匹配,则selected
菜单项的prop应该为true。为了获得当前位置作为组件中的道具,您将需要使用的withRouter
HOC react-router
。
这是验证其工作原理的完整代码。 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);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3786 次 |
最近记录: |