简体   繁体   English

如何 map 列出并在其中传递 @material-ui/icons

[英]How to map list and passing @material-ui/icons in it

I want to pass @material-ui/icons into the MenuList我想将 @material-ui/icons 传递到 MenuList

first I declare a list:首先我声明一个列表:

const notifications = [
  {
    id: 0,
    avatarIcon: "<DashboardIcon/>",
    message: "Hey! How is it going?",
    time: "9:32",
  },
  {
    id: 1,
    avatarIcon: "<MenuIcon/>",
    message: "Check out my new Dashboard",
    time: "9:18",
  },
  {
    id: 2,
    avatarIcon: "<WifiIcon/>",
    message: "I want rearrange the appointment",
    time: "9:15",
  },
  {
    id: 3,
    avatarIcon: "<DashboardIcon/>",
    message: "Good news from sale department",
    time: "9:09",
  },
];

in my render I wrote:在我的渲染中我写道:

{notifications.map(message => (
              <MenuItem key={message.id} className={classes.messageNotification}>
                <div className={classes.messageNotificationSide}>

                  <Avatar className={classes.pinkAvatar}>
                    <DashboardIcon/>          {/* <----- here */}
                  </Avatar>

                  <Typography size="sm" color="secondary">
                    {message.time}
                  </Typography>
                </div>
                ...
              </MenuItem>
            ))}

is there any solution to this problem like {message.avatarIcon}... thanks有没有解决这个问题的方法,比如{message.avatarIcon} ...谢谢

You have to import your icons from the library first then reference it to the object Like您必须先从库中导入您的图标,然后将其引用到 object Like

import {
 DashboardIcon
} from "material-ui/icons"

Then Reference it like然后像这样引用它

const Icons = [
  {
    id: 1,
    name: "Dashboard",
    description:"icon",
    icon: DashboardIcon,
  }]

Now inside your map use an icon like this.现在在您的 map 中使用这样的图标。

 {Icons.map(list=>(
      <div key={list.id}>
        <list.icon className="w-8 h-8" />       
      </div>  
 ))}

I would change your avatarIcon value like this:我会像这样更改您的 avatarIcon 值:

const notifications = [
  {
    id: 0,
    avatarIcon: "DASHBOARD",
    message: "Hey! How is it going?",
    time: "9:32",
  },
  {
    id: 1,
    avatarIcon: "MENU",
    message: "Check out my new Dashboard",
    time: "9:18",
  },
  {
    id: 2,
    avatarIcon: "WIFI",
    message: "I want rearrange the appointment",
    time: "9:15",
  },
  {
    id: 3,
    avatarIcon: "DASHBOARD",
    message: "Good news from sale department",
    time: "9:09",
  },
];

Then create a method that uses a switch case to determine which component to render:然后创建一个使用 switch case 来确定要渲染的组件的方法:

...
getAvataricon(icon) {
  swicth(icon) {
    case 'DASHBOARD':
      return (<DashboardIcon/>);
    case 'WIFI':
      return (<WifiIcon/>);
    case 'MENU':
      return (<MenuIcon/>);
    default:
      return (<WhateverIcon/>);
  }
}
...
{notifications.map(message => (
              <MenuItem key={message.id} className={classes.messageNotification}>
                <div className={classes.messageNotificationSide}>

                  <Avatar className={classes.pinkAvatar}>
                    {getAvatarIcon(message.avatarIcon)}          {/* <----- here */}
                  </Avatar>

                  <Typography size="sm" color="secondary">
                    {message.time}
                  </Typography>
                </div>
                ...
              </MenuItem>
            ))}

PS I'm an Angular dev, did some things with React so I don't know for sure if my JSX completely clean or correct;) PS 我是 Angular 开发人员,用 React 做了一些事情,所以我不确定我的 JSX 是否完全干净或正确;)

So first off take the string quotations off the icons.所以首先从图标上取下字符串引号。 const notifications = [ { id: 0, avatarIcon: , message: "Hey? How is it going,": time: "9,32", }: { id, 1: avatarIcon, : message, "Check out my new Dashboard": time: "9,18", }: { id, 2: avatarIcon, : message, "I want rearrange the appointment": time: "9,15", }: { id, 3: avatarIcon, : message, "Good news from sale department": time: "9,09", }; const notifications = [ { id: 0, avatarIcon: , message: "嘿?怎么样了,": time: "9,32", }: { id, 1: avatarIcon, : message, "看看我的新仪表板": time: "9,18", }: { id, 2: avatarIcon, : message, "我要重新安排约会": time: "9,15", }: { id, 3: avatarIcon, : message, "销售部的好消息": time: "9,09", }; ]; ];

 {notifications.map(message => (
 Let Icon = message.avatarIcon;
             return(  <MenuItem key={message.id} 
className={classes.messageNotification}>
                 <div className= 
 {classes.messageNotificationSide}>

                   <Avatar className={classes.pinkAvatar}>
                     <Icon/>          
                   </Avatar>

                   <Typography size="sm" color="secondary">
                     {message.time}
                   </Typography>
                  </div>
           
               </MenuItem>)
             ))}

So bit of back story to my answer, In reactjs Jsx is used which allow components to be assigned into json objects and be passed.所以我的答案的背景故事,在 reactjs 中使用了 Jsx,它允许将组件分配到 json 对象中并被传递。 I've personally used this method to dynamically load content from an index.js file story various different components.我个人使用此方法从 index.js 文件故事中动态加载各种不同组件的内容。

replace this avatarIcon: "DASHBOARD" ,with this avatarIcon: (<DashBoardIcon/>) hence by just feeding in the actual icon用这个avatarIcon: (<DashBoardIcon/>)替换这个avatarIcon: "DASHBOARD" ,因此只需输入实际的图标

...
const notifications = [
  {
    id: 0,
    avatarIcon: (<DashboardIcon/>),
    message: "Hey! How is it going?",
    time: "9:32",
  },
  {
    id: 1,
    avatarIcon: (<MenuIcon/>),
    message: "Check out my new Dashboard",
    time: "9:18",
  },
  {
    id: 2,
    avatarIcon: (<WifiIcon/>),
    message: "I want rearrange the appointment",
    time: "9:15",
  },
  {
    id: 3,
    avatarIcon: (<DashboardIcon/>),
    message: "Good news from sale department",
    time: "9:09",
  },
];...
<List>
    {notifications.map((item, index) => (
        <ListItem button key={item}>
            <ListItemIcon>{item.avatarIcon}</ListItemIcon>
            <ListItemText primary={item.message} />
        </ListItem>
    ))}
</List>

Modify the list with required icons and map the data from the dict as above.用所需的图标和 map 修改列表中的 dict 数据,如上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM