how to use icon in Ant-design/icons with V4

3.4k views Asked by At

I have tried to make a menu, so i create a menuList to config the menu with getMenuNodes(), but Ant framework has been upgraded from v3 to v4 which the icon method has been changed. they are now using icon={<PieChartOutlined />} to instead of icon='PieChartOutlined', everything is working well, the icon area shows the word <PieChartOutlined />right now. i do not know why it happened, please help me to solve this problem.

left-navigation.js

import React, { Component } from 'react';
import { Link } from 'react-router-dom';
import logo from '../../assets/images/logo.png';
import './index.less';
import { Menu } from 'antd';
import { PieChartOutlined } from '@ant-design/icons';
import menuList from '../../config/menuConfig';
const { SubMenu } = Menu;
export default class LeftNav extends Component {
  getMenuNodes = menuList => {
    return menuList.map(item => {
      if (!item.children) {
        return (
          <Menu.Item key={item.key} icon={item.icon}>
            <Link to={item.key}>{item.title}</Link>
          </Menu.Item>
        );
      } else {
        return (
          <SubMenu key={item.key} icon={item.icon} title={item.title}>
            {this.getMenuNodes(item.children)}
          </SubMenu>
        );
      }
    });
  };
  render() {
    return (
      <div className="left-nav">
        <Link to="./" className="left-nav-header">
          <img src={logo} alt="" />
          <h1>Backend System</h1>
        </Link>
        <Menu
          mode="inline"
          theme="dark"
        >
          {this.getMenuNodes(menuList)}
        </Menu>
      </div>
    );
  }
}

menuList.js

const menuList = [
  {
    title: 'Home', 
    key: '/home', 
    icon: '<PieChartOutlined />', 
  },
  {
    title: 'Item',
    key: '/products',
    icon: '<PieChartOutlined />',
    children: [
      {
        title: 'Category Control',
        key: '/category',
        icon: '<PieChartOutlined />',
      },
      {
        title: 'Product Control',
        key: '/product',
        icon: '<PieChartOutlined />',
      },
    ],
  },
  {
    title: 'User Control',
    key: '/user',
    icon: '<PieChartOutlined />',
  },
  {
    title: 'Role Control',
    key: '/role',
    icon: '<PieChartOutlined />',
  },
  {
    title: 'Diagram',
    key: '/charts',
    icon: '<PieChartOutlined />',
    children: [
      {
        title: 'Bar',
        key: '/charts/bar',
        icon: '<PieChartOutlined />',
      },
      {
        title: 'Line',
        key: '/charts/line',
        icon: '<PieChartOutlined />',
      },
      {
        title: 'Pie',
        key: '/charts/pie',
        icon: '<PieChartOutlined />',
      },
    ],
  },
];
export default menuList;
2

There are 2 answers

1
Emilio On BEST ANSWER

You are passing a string of '<PieChartOutlined />', you need to pass the component directly.

import { PieChartOutlined } from '@ant-design/icons';

and:

      {
        title: 'Product Control',
        key: '/product',
        icon: <PieChartOutlined />,
      },

You'll need to install ant-design/icons if you haven't already:

npm install --save @ant-design/icons

0
cuongdevjs On

Since reason performance on the previous version, antd team was apply tree-shaking to use icon. More detail, you can check https://ant.design/docs/react/migration-v4