【发布时间】: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