【问题标题】:Mapping a different icon depending on property value in React根据 React 中的属性值映射不同的图标
【发布时间】:2018-12-11 15:16:25
【问题描述】:

https://codesandbox.io/s/r546o8v0kq

我上面的沙箱显示了一组项目的基本映射。这将形成一个注释、日期和图标列表,具体取决于它是什么类型的项目。

我正在使用一些逻辑来映射每个项目以找出它是什么值,基于此我为该值分配一个字符串以完成字体真棒徽标的类型。

const noteType = _.uniq(notes.map(value => value.intelType));
const noteIcon = [
  `${noteType}`.toUpperCase() == "EDUCATION"
    ? "paper-plane"
    : `${noteType}`.toUpperCase() == "ELIGIBILITY"
    ? "heart"
    : `${noteType}`.toUpperCase() == "GENERAL"
    ? "twitter"
    : null
];

如果“intelType”的值为“education”,它将返回“paper-plane”字符串来完成图标。例如fa fa-${noteIcon}

    <List>
      {notes.map((note, index) =>
        note !== "" ? (
          <React.Fragment>
            <ListItem className="pl-0">
              <i class={`fa fa-${noteIcon}`} aria-hidden="true" />
              <ListItemText
                secondary={moment(note.createdAt).format("DD-MMM-YYYY")}
              />
            </ListItem>
            <p>{note.note}</p>
            <Divider />
          </React.Fragment>
        ) : null
      )}
    </List>

它没有被映射并返回所有三个值,它不符合任何条件,因此根据请求返回 null。我有点不知道下一步该做什么。

【问题讨论】:

    标签: javascript reactjs lodash


    【解决方案1】:

    您可以定义一个将 intel 类型映射到图标名称的对象:

    const noteIconMap = 
          { "EDUCATION": "paper-plane",
            "ELIGIBILITY": "heart",
            "GENERAL": "twitter",
          };
    

    并在渲染中以这种方式查找:

    <i class={`fa fa-${noteIconMap[note.intelType.toUpperCase()]}`} aria-hidden="true" />
    

    不过,请注意,如果note 可以有intelType 未定义,toUpperCase 调用将引发错误。

    这里是工作修改沙箱的链接: https://codesandbox.io/s/ojz2lzz03z

    【讨论】:

      【解决方案2】:

      我不确定这段代码是怎么回事:

      const noteIcon = [
        `${noteType}`.toUpperCase() == "EDUCATION"
          ? "paper-plane"
          : `${noteType}`.toUpperCase() == "ELIGIBILITY"
          ? "heart"
          : `${noteType}`.toUpperCase() == "GENERAL"
          ? "twitter"
          : null
      ]
      

      但对我来说,将这些信息存储为对象然后以这种方式访问​​图标类型更有意义。

      const icons = {
          EDUCATION: 'paper-plane',
          ELIGIBILITY: 'heart',
          GENERAL: 'twitter'
      }
      

      然后去:

      icons[noteType]
      

      【讨论】:

        【解决方案3】:

        您需要在地图中为每个note.intelType 获取一个图标。由于您传递的是一个 id 数组,因此没有一个图标匹配,结果始终为 null

        一个简单的解决方案是创建图标类型的Map (iconsMap),并使用note.intelType 从地图中获取图标。

        顺便说一句 - 目前note.intelType我们总是大写,所以你不需要转换它。

        sandbox

        const iconsMap = new Map([
          ['EDUCATION', 'paper-plane'],
          ['ELIGIBILITY', 'heart'],
          ['GENERAL', 'twitter'],
        ]);
        
        class Notes extends Component {
          constructor(props) {
            super(props);
            this.state = {};
          }
        
          render() {
            const { notes, classes } = this.props;
        
            return (
              <React.Fragment>
                <List>
                  {notes.map((note, index) =>
                    note !== "" ? (
                      <React.Fragment>
                        <ListItem className="pl-0">
                          <i class={`fa fa-${iconsMap.get(note.intelType)}`} aria-hidden="true" />
                          <ListItemText
                            secondary={moment(note.createdAt).format("DD-MMM-YYYY")}
                          />
                        </ListItem>
                        <p>{note.note}</p>
                        <Divider />
                      </React.Fragment>
                    ) : null
                  )}
                </List>
              </React.Fragment>
            );
          }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2015-04-15
          • 2019-04-12
          • 2023-03-25
          • 2012-09-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多