【问题标题】:React - FontAwesomeIcon from a variableReact - 来自变量的 FontAwesomeIcon
【发布时间】:2019-03-16 14:21:50
【问题描述】:

我在一个函数上有条件地选择图标,但是当我尝试渲染它时失败了......

这就是我正在尝试的:

  getResult = item => {
    let variant;
    let faIcon;
    if (item.result === "UNSTABLE") {
      variant = "#ffc107";
      faIcon = { faExclamationTriangle }
    } else if (item.result === "SUCCESS") {
      variant = "#009688";
      faIcon = { faCheckCircle }
    } 

    return <FontAwesomeIcon size="2x" icon={faIcon} color={variant} />;
  };
}

也试过这个:

  getResult = item => {
    let variant;
    let faIcon;
    if (item.result === "UNSTABLE") {
      variant = "#ffc107";
      faIcon = <faExclamationTriangle/>;
    } else if (item.result === "SUCCESS") {
      variant = "#009688";
      faIcon = <faCheckCircle/>
    } 

    return <FontAwesomeIcon size="2x" icon={faIcon} color={variant} />;
  };
}

这只能以这种方式工作,但它是丑陋的地狱......

getResult = item => { 
    let variant;
    let faIcon;
    if (item.result === "UNSTABLE") {
      variant = "#ffc107";
      faIcon = { faExclamationTriangle }.faExclamationTriangle;
    } else if (item.result === "SUCCESS") {
      variant = "#009688";
      faIcon = { faCheckCircle }.faCheckCircle;
    } 

    return <FontAwesomeIcon size="2x" icon={faIcon} color={variant} />;
  };
}

当然问题是缺乏一些概念......

【问题讨论】:

    标签: reactjs refactoring font-awesome jsx


    【解决方案1】:

    我想换

    faIcon = { faExclamationTriangle }.faExclamationTriangle;

    faIcon = { faCheckCircle }.faCheckCircle;

    与

    faIcon = faExclamationTriangle;

    faIcon = faCheckCircle;

    会解决你的问题。

    【讨论】:

    • 成功了!谢谢!你能解释一下,或者展示一些文档,为什么这个工作和我的测试没有?
    • 你用大括号包裹 faExclamationTriangle, faCheckCircle,这是 ES6 对象字面量的简写语法。 { faExclamationTriangle } 与 { faExclamationTriangle: faExclamationTriangle }. 相同
    【解决方案2】:

    您可以通过将状态移动到它自己的对象中来制作代码declarative(就像 React 一样),使其可配置(而不是命令式ifs)。

    const iconMap = {
        "UNSTABLE": {
            color: "#ffc107",
            icon:  { faExclamationTriangle }
        },
        "SUCCESS": {
            color: "#009688",
            icon:  { faCheckCircle }
        },
    }
    
    
    getResult = item => <FontAwesomeIcon size="2x" 
            color={iconMap[item.result].color} 
            icon={iconMap[item.result].icon} />;
        // if you want to pass all props...
        // <FontAwesomeIcon size="2x" {...iconMap[item.result]} />;
    

    添加一个新状态的支持就是在iconMap 中添加一个新对象。

    事实上,上面类似于Material-UI & Styled-Components 让你更新你的主题。

    【讨论】:

    • 其他答案有效,但我认为这是正确的方法!谢谢!
    • 啊,我不知道是 {...} 导致了这个问题......无论如何,很高兴你能够解决这个问题
    【解决方案3】:

    这应该可行:

    getResult = item => { 
        let variant;
        let faIcon;
        if (item.result === "UNSTABLE") {
            return <FontAwesomeIcon size="2x" icon={{ faExclamationTriangle }.faExclamationTriangle} color={variant} />;
        } else if (item.result === "SUCCESS") {
            return <FontAwesomeIcon size="2x" icon={{ faCheckCircle }.faCheckCircle} color={variant} />;
        } 
      };
    }
    

    【讨论】:

    • 是的,当然......但这是我试图避免的......我有很多选择,每次重复 没有意义
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-28
    • 1970-01-01
    • 2012-03-15
    • 1970-01-01
    • 2021-05-07
    相关资源
    最近更新 更多