![](/img/trans.png)
[英]I'm rendering 5 columns from a single parent component and they all have different icons. How do i conditionally render them in react?
[英]How do I conditionally render font awesome icons in react
我正在尝试有条件地从字体真棒中呈现空心和满心图标。
我正在学习教程,但他们使用的是字体真棒 4.7
import React, { Component } from "react";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faHeart as faEmptyHeart } from "@fortawesome/free-regular-svg-icons";
import { faHeart as faSolidHeart } from "@fortawesome/free-solid-svg-icons";
//Input: liked: boolean
//Outpur: onClick
class Like extends Component {
render() {
let classes = { faSolidHeart };
if (!this.props.liked) classes = { faEmptyHeart };
return <FontAwesomeIcon icon={classes} />;
}
}
export default Like;
FontAwesomeIcon
中的icon
FontAwesomeIcon
不需要一个对象,而是一个图标。 你实际上做的是传递一个key
等于faSolidHeart
的对象,它的值作为faHeart
图标。
只需删除大括号。 下面是一个简化和缩短的解决方案:
class Like extends Component {
render() {
return <FontAwesomeIcon icon={this.props.liked ? faSolidHeart : faEmptyHeart} />;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.