简体   繁体   English

来自带有类名而不是组件的反应图标的图标

[英]Icons from react-icons with classes name instead of component

Is there a way in React Icons to specify my icons as a string, for example for the class name? React Icons 中有没有办法将我的图标指定为字符串,例如 class 名称?

Currently I am using a React UI library which requires the names of the icons to be passed as their classes.目前我正在使用 React UI 库,它需要将图标的名称作为它们的类传递。

I know that you can use icons using react-icons as follows:我知道您可以使用 react-icons 来使用图标,如下所示:

<FaArrowLeft/>

However, I would need the following icon name: "fa-arrow-left".但是,我需要以下图标名称:“fa-arrow-left”。

Is there any way I can get the class name instead of using the component?有什么方法可以获得 class 名称而不是使用该组件?

I haven't used React Icons specifically but I've gotten the result you are after by using bootstrap and font-awesome.我没有专门使用 React Icons,但我已经通过使用 bootstrap 和 font-awesome 得到了你想要的结果。 Install bootstrap and font-awesome in you project.在您的项目中安装 bootstrap 和 font-awesome。

yarn add bootstrap
yarn add font-awesome

Or或者

   npm install bootstrap
   npm install font-awesome

Import them (I import to my index.js file but where ever is the root of your app)导入它们(我导入到我的 index.js 文件,但你的应用程序的根在哪里)

import 'bootstrap/dist/css/bootstrap.min.css';
import 'font-awesome/css/font-awesome.min.css';

Then you can use class names like然后你可以使用 class 这样的名字

'fa fa-sm fa-eye'

for example to get a small eye icon.例如得到一个小眼睛图标。

I am not sure which library you are using, but I'm assuming it's 'react-icons/fa'.我不确定您使用的是哪个库,但我假设它是“react-icons/fa”。 It is possible to use it by string but after you import them all which might effect your bundle size by ~30MB extra which is not small size.可以按字符串使用它,但在你导入它们之后,这可能会影响你的包大小 ~30MB 额外,这不是小尺寸。 Here is an example how to do it with custom component:以下是如何使用自定义组件执行此操作的示例:

 import * as Icons from "react-icons/fa";
    
    const CustomFaIcon = ({ name }) => {
      const FaIcon = Icons[name];
      if (!FaIcon) return <p>Icon not found!</p>;
    
      return <FaIcon />;
    };
    
    export default function App() {
      return (
        <div className="App">
          <CustomFaIcon name="Your String" />
        </div>
      );
    }

I also suggest that you check out react-fontawesome library, you might end up with lower bundle size if you use it corrently, here you can find some examples how to do it: https://github.com/FortAwesome/react-fontawesome/blob/master/examples/create-react-app-typescript/src/App.tsx我还建议您查看 react-fontawesome 库,如果正确使用它,您最终可能会得到更小的包大小,在这里您可以找到一些示例: https://github.com/FortAwesome/react-fontawesome /blob/master/examples/create-react-app-typescript/src/App.tsx

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

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