简体   繁体   English

React TS - 如何映射 FontAwesome 图标并显示它们

[英]React TS - How to map through FontAwesome icons and display them

I'm trying to create a reusable component that allows me to pass in the href , target , rel attributes along with the type of FontAwesome Icon I would like to use.我正在尝试创建一个可重用的组件,它允许我传入hreftargetrel属性以及我想使用的 FontAwesome 图标类型。 I'd like to be able to pass as many icons as I would like into this list and it would map through them, using the Social component as a template.我希望能够将尽可能多的图标传递到这个列表中,它会使用社交组件作为模板来映射它们。

I would like the map to happen within the Social.tsx file, then I can simply just add the component anywhere in my project and then pass the props to it.我希望地图发生在Social.tsx文件中,然后我可以简单地将组件添加到我的项目中的任何位置,然后将道具传递给它。

Something like this:像这样的东西:

<Social
  icons={[
    { icon: "facebook", href: "", target: "", rel: "" },
    { icon: "twitter", href: "", target: "", rel: "" },
    { icon: "discord", href: "", target: "", rel: "" }
  ]}
/>

I currently have my component:我目前有我的组件:

Social.tsx 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应用程序.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 />;

How can I adapt this to do what I described above?我怎样才能适应它来做我上面描述的事情?

Here's a CodeSandBox这是一个CodeSandBox

Thanks for any help in advance!提前感谢您的任何帮助!

So, I think the problem is with structuring the props that you want to pass.所以,我认为问题在于构建你想要传递的道具。

For instance, we will pass an array of type IconProp then you can easily map over it.例如,我们将传递一个IconProp类型的数组,然后您可以轻松地对其进行映射。

Now you can make your own interface and pass props of this type现在你可以制作自己的界面并传递这种类型的道具

interface myType {
  href?: string;
  target?: string;
  rel?: string;
  icon: IconProp;
}
const dummyProp: myType[] = [
  { icon: ["fab", "discord"] },
  { icon: ["fab", "discord"] },
  { icon: ["fab", "discord"] },
  { icon: ["fab", "discord"] }
];

const Social: React.FC<ISocial> = props => {
  return (
    <div>
      {dummyProp.map(p => (
        <a href={p.href} target={p.target} rel={p.rel}>
          <FontAwesomeIcon icon={p.icon} />
        </a>
      ))}
    </div>
  );

Have a list of strings with icon names and use map if required to have list.有一个带有图标名称的字符串列表,如果需要列表,请使用map

export default function App() {
  return (
    <Social
      icons={[
        { icon: "facebook", href: "", target: "", rel: "" },
        { icon: "twitter", href: "", target: "", rel: "" },
        { icon: "discord", href: "", target: "", rel: "" }
      ]}
    />
  );
}

Changes to Social Component.社会成分的变化。 Just add new prop like name只需添加新的道具,如name

import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import * as React from "react";
import { IconName } from "@fortawesome/fontawesome-svg-core";

interface ISocial {
  href?: string;
  target?: string;
  rel?: string;
  icon: IconName;
}

interface ISocialList {
  icons: Array<ISocial>;
}

const Social: React.FC<ISocialList> = props => {
  const { icons } = props;
  return (
    <div>
      {icons.map(item => {
        return (
          <a href={item.href} target={item.target} rel={item.rel}>
            <FontAwesomeIcon icon={["fab", item.icon]} />
          </a>
        );
      })}
    </div>
  );
};

export default Social;

Code changes to your sandbox https://codesandbox.io/s/headless-bush-br14l对您的沙箱的代码更改https://codesandbox.io/s/headless-bush-br14l

 //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 one. // The Social component would be able to map through them // and display them. const iconList = [ { name: "twitter", href: "https://www.google.com", target: "_blank" }, { name: "discord", href: "https://www.google.com", target: "_blank" } ]; return ( <div> {iconList.map(ico => { return <Social icon={ico.name} href={ico.href} />; })} </div> ); } //Social.tsx import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import * as React from "react"; interface ISocial { href?: string; target?: string; rel?: string; icon: String; } const Social: React.FC<ISocial> = props => { return ( <div> <a href={props.href} target={props.target} rel={props.rel}> <FontAwesomeIcon icon={["fab", props.icon]} /> </a> </div> ); }; export default Social;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM