如何映射列表并在其中传递@ material-ui / icons

我想将@ material-ui / icons传递到MenuList

首先我声明一个列表:

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,message: "Good news from sale department",time: "9:09",];

我在渲染中写道:

{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>
            ))}

有没有解决此问题的方法 像{message.avatarIcon} ... 谢谢

Swdream2008 回答:如何映射列表并在其中传递@ material-ui / icons

我会这样更改您的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,message: "Good news from sale department",time: "9:09",];

然后创建一个使用开关盒来确定要渲染的组件的方法:

...
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>
            ))}

P.S。我是Angular开发人员,使用React做过一些事情,所以我不确定我的JSX是否完全干净或正确;)

,

因此,首先请删除图标中的字符串引号。 const通知= [ { ID:0, 头像图标:, 消息:“嘿!怎么样了?”, 时间:“ 9:32”, }, { 编号:1 头像图标:, 消息:“签出我的新仪表盘”, 时间:“ 9:18”, }, { 编号:2 头像图标:, 消息:“我想重新安排约会”, 时间:“ 9:15”, }, { id:3, 头像图标:, 消息:“来自销售部门的好消息”, 时间:“ 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>)
             ))}

关于我的回答,可以回溯一点,在reactjs中使用了Jsx,它允许将组件分配给json对象并进行传递。我亲自使用此方法从index.js文件中的各种不同组件动态加载内容。

,

用这个avatarIcon: "DASHBOARD"替换这个avatarIcon: (<DashBoardIcon/>),因此只需 输入实际图标

,
...
const notifications = [
  {
    id: 0,avatarIcon: (<DashboardIcon/>),avatarIcon: (<MenuIcon/>),avatarIcon: (<WifiIcon/>),];...
<List>
    {notifications.map((item,index) => (
        <ListItem button key={item}>
            <ListItemIcon>{item.avatarIcon}</ListItemIcon>
            <ListItemText primary={item.message} />
        </ListItem>
    ))}
</List>

使用所需的图标修改列表并从字典中映射数据,如上。

本文链接:https://www.f2er.com/3158611.html

大家都在问