react的antd左侧菜单及刷新选中状态

本文详细介绍了如何在React应用中使用Ant Design库创建左侧菜单,并在页面刷新后保持菜单选中状态。通过利用React的状态管理和Antd的Menu组件特性,实现动态设置菜单高亮。
摘要由CSDN通过智能技术生成
数据格式
[
    {
        "title": "首页",
        "icon": "home",
        "path": "/home"
    },
    {
        "title": "商品",
        "icon": "home",
        "path": "/catalogProduct",
        "children": [
            {
                "title": "品类管理",
                "icon": "home",
                "path": "/catalog"
            },
            {
                "title": "商品管理",
                "icon": "home",
                "path": "/product"
            }
        ]
    },
    {
        "title": "用户管理",
        "icon": "user",
        "path": "/user"
    },
    {
        "title": "角色管理",
        "icon": "role",
        "path": "/role"
    },
    {
        "title": "图形图标",
以下是基于Ant Design Pro的动态菜单生成的完整方法附代码,可以实现左侧多级菜单: ```javascript import React from 'react';import { Menu, Icon } from 'antd'; import { Link } from 'dva/router'; import { getMenuData } from '../common/menu'; const { SubMenu } = Menu; export default class SiderMenu extends React.Component { constructor(props) { super(props); this.menus = getMenuData(); this.state = { openKeys: this.getDefaultCollapsedSubMenus(props), }; } componentWillReceiveProps(nextProps) { const { pathname } = nextProps.location; if (this.props.location.pathname !== pathname) { this.setState({ openKeys: this.getDefaultCollapsedSubMenus(nextProps), }); } } /** * Convert pathname to openKeys * /list/search/articles = > ['list','/list/search'] * @param props */ getDefaultCollapsedSubMenus(props) { const { location: { pathname } } = props || this.props; // eg. /list/search/articles = > ['','list','search','articles'] const snippets = pathname.split('/').slice(1, -1); const currentPathSnippets = snippets.map((item, index) => { const arr = snippets.slice(0, index + 1); return `/${arr.join('/')}`; }); let currentMenuSelectedKeys = []; currentPathSnippets.forEach((item) => { currentMenuSelectedKeys = currentMenuSelectedKeys.concat(this.getSelectedMenuKeys(item)); }); if (currentMenuSelectedKeys.length === 0) { return ['dashboard']; } return currentMenuSelectedKeys; } getFlatMenuKeys(menus) { let keys = []; menus.forEach((item) => { if (item.children) { keys = keys.concat(this.getFlatMenuKeys(item.children)); } keys.push(item.path); }); return keys; } getSelectedMenuKeys = (path) => { const flatMenuKeys = this.getFlatMenuKeys(this.menus); const key = flatMenuKeys.filter(item => (item && path.indexOf(item) === 0)); return key; } isMainMenu = (key) => { const { menus } = this; return menus.some(item => key && (item.key === key || item.path === key)); } handleOpenChange = (openKeys) => { const lastOpenKey = openKeys[openKeys.length - 1]; const moreThanOne = openKeys.filter(openKey => this.isMainMenu(openKey)).length > 1; this.setState({ openKeys: moreThanOne ? [lastOpenKey] : [...openKeys], }); } render() { const { logo, collapsed, onCollapse } = this.props; const { openKeys } = this.state; // Don't show popup menu when it is been collapsed const menuProps = collapsed ? {} : { openKeys, }; // if pathname can't match, use the nearest parent's key let selectedKeys = this.getSelectedMenuKeys(this.props.location.pathname); if (!selectedKeys.length) { selectedKeys = [openKeys[openKeys.length - 1]]; } return ( <div> <div className="logo" id="logo"> <Link to="/"> <img src={logo} alt="logo" /> <h1>Ant Design Pro</h1> </Link> </div> <Menu key="Menu" theme="dark" mode="inline" {...menuProps} onOpenChange={this.handleOpenChange} selectedKeys={selectedKeys} style={{ padding: '16px 0', width: '100%' }} > {this.menus .filter(item => !item.hideInMenu) .map((item) => { if (item.children && item.children.some(child => !child.hideInMenu)) { return ( <SubMenu key={item.key} title={<span><Icon type={item.icon} /><span>{item.name}</span></span>} > {item.children.map(child => child.hideInMenu ? null : ( <Menu.Item key={child.key}> <Link to={child.path}> <span>{child.name}</span> </Link> </Menu.Item> ))} </SubMenu> ); } return ( <Menu.Item key={item.key}> <Link to={item.path}> <Icon type={item.icon} /> <span>{item.name}</span> </Link> </Menu.Item> ); })} </Menu> </div> ); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值