【发布时间】:2020-06-02 16:24:22
【问题描述】:
我正在尝试创建一个可重用组件,它允许我传入href、target、rel 属性以及我想使用的 FontAwesome Icon 类型。我希望能够将任意数量的图标传递到此列表中,并使用 Social 组件作为模板映射它们。
我希望地图出现在 Social.tsx 文件中,然后我可以简单地将组件添加到项目中的任何位置,然后将道具传递给它。
类似这样的:
<Social
icons={[
{ icon: "facebook", href: "", target: "", rel: "" },
{ icon: "twitter", href: "", target: "", rel: "" },
{ icon: "discord", href: "", target: "", rel: "" }
]}
/>
我目前有我的组件:
Social.tsx
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import * as React from "react";
interface ISocial {
href?: string
target?: string
rel?: string
}
const Social: React.FC<ISocial> = (props) => {
return (
<div>
<a href={props.href} target={props.target} rel={props.rel}>
{/* I don't want to hardcode the icon, but dynamically pass it as a prop */}
<FontAwesomeIcon icon={["fab", "discord"]} />
</a>
</div>
);
};
export default Social;
App.tsx
import * as React from "react";
import "./styles.css";
import Social from "./components/Social";
export default function App() {
// I'd like to be able to pass in a list of different icons
// followed by the href, target, rel, etc for each item.
// The Social component would be able to map through them
// and display them.
return <Social />;
我怎样才能适应它来做我上面描述的事情?
提前感谢您的帮助!
【问题讨论】:
标签: javascript reactjs typescript font-awesome