简体   繁体   English

整合react和react-fontawesome

[英]integrate react and react-fontawesome

The react-fontawesome documentation suggests to create a custom library, to only include the icons one needs. react-fontawesome文档建议创建一个自定义库,仅包含一个需要的图标。

They say to do as follow: 他们说要这样做:

  • Add required packages. 添加所需的软件包。
  • Create custom library like so: 创建自定义库,如下所示:

     import { library } from '@fortawesome/fontawesome-svg-core'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import { faStroopwafel } from '@fortawesome/free-solid-svg-icons'; library.add(faStroopwafel); 
  • Use custom library like so: 使用自定义库,如下所示:

      import React from 'react' import { FontAwesomeIcon } from '@fortawesome/react-fontawesome' ... return(<FontAwesomeIcon icon="stroopwafel" />); 

The question is about the 2nd step: is it possible to create a separate file for the library creation? 问题与第二步有关:是否可以为库创建创建单独的文件? I don't like the idea of having a long list of icons in my application entry point. 我不喜欢在应用程序入口点设置一长串图标的想法。

What confuses me is that there isn't anything to export, since is it is only a function call (library.add). 令我困惑的是,没有什么可导出的,因为它只是一个函数调用(library.add)。 Should I export a self calling function that executes the library creation? 我是否应该导出执行库创建的自调用函数?

编辑71yv4pw3nq

Maybe you can write a file that contains your filled library : 也许您可以编写一个包含已填充库的文件:

import { library } from '@fortawesome/fontawesome-svg-core';
import { faStroopwafel } from '@fortawesome/free-solid-svg-icons';

library.add(faStroopwafel);

And in your main file, only import this library and FontAwesomeIcon : 并且在您的主文件中,仅导入该库和FontAwesomeIcon

import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import "./MyLib"

function App() {
  return (
    <div className="App">
      <FontAwesomeIcon icon="stroopwafel" />
    </div>
  );
}

Edit : Thanks to @mwilkerson for the improved version 编辑 :感谢@mwilkerson的改进版本

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

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