【问题标题】:React TS - How to map through FontAwesome icons and display themReact TS - 如何通过 FontAwesome 图标映射并显示它们
【发布时间】:2020-06-02 16:24:22
【问题描述】:

我正在尝试创建一个可重用组件,它允许我传入hreftargetrel 属性以及我想使用的 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 />;

我怎样才能适应它来做我上面描述的事情?

这是CodeSandBox

提前感谢您的帮助!

【问题讨论】:

    标签: javascript reactjs typescript font-awesome


    【解决方案1】:

    有一个带有图标名称的字符串列表,如果需要有列表,请使用map

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

    对社交组件的更改。只需添加新的道具,如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;
    

    您的沙盒代码更改https://codesandbox.io/s/headless-bush-br14l

    【讨论】:

    • 您好,感谢您的快速回复!如果可能的话,我希望在 Social 组件中完成地图?
    • @Jordan,更新了答案和沙盒。希望这会有所帮助。
    • 谢谢,这几乎就是我想要的,但我希望能够将数组直接传递给标签本身,请参阅我的问题作为示例。
    • @Jordan,更新了答案和沙盒。对 icons 属性值稍作更改以具有有效数组。
    • 我们正在取得进展! :) 差不多了,但它在第 21 行 icon={} 上为 Social.tsx 给出了类型错误?
    【解决方案2】:

    所以,我认为问题在于构建你想要传递的道具。

    例如,我们将传递一个IconProp 类型的数组,然后您可以轻松地对其进行映射。

    现在您可以制作自己的界面并传递这种类型的道具

    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>
      );
    

    【讨论】:

    • 您好,感谢您的回复。我想从&lt;Social /&gt; 传递数组,如果它在组件本身中硬编码,它就不是很动态。
    • 是的,我知道这只是一个例子,无论如何你都应该得到一个你想要传递的图标列表,所以在你需要的任何组件中构建类似这个界面的东西并将它传递下去作为道具。
    • 对不起,我很困惑,你能举个例子来说明你所描述的吗?
    • checkout this codesandbox.io/s/boring-chebyshev-8eqim 好吧,我不知道为什么facebook和twitter图标没有显示,但没关系
    【解决方案3】:

    //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;

    【讨论】:

    • 您好。我希望地图发生在 &lt;Social /&gt; 本身内,理想情况下只需将数组传递给 &lt;Social icons=[{icon="", href="", target="", rel=""}, icon="", href="", target="", rel=""] /&gt; 标记。
    • 这样更好,地图现在在组件内部,但我可以像上面提到的那样将 iconList 数组传递给 标记吗?
    猜你喜欢
    • 2021-12-22
    • 2017-07-20
    • 1970-01-01
    • 2018-07-30
    • 2020-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-11
    相关资源
    最近更新 更多