简体   繁体   English

"React 本机矢量图标 - 动态类型"

[英]React native vector icons - dynamic type

I'm using different types of react native vector icons - Material and FontAwesome depending on availability of a particular icon.我正在使用不同类型的反应原生矢量图标 - Material 和 FontAwesome,具体取决于特定图标的可用性。 I wanted to create a common component that wraps usage of the icons across the app.我想创建一个通用组件来包装整个应用程序中图标的使用。 So far I have:到目前为止,我有:

import React from 'react';
import Icon from "react-native-vector-icons/FontAwesome";
import {theme} from "../../../styles/style";

/**
 * Common reusable icon component
 * @param props
 * @returns {*}
 */
const icon = (props) => {
    return (
        <Icon
            size={theme.SIZE_ICON}
            color={theme.BACKGROUND_ICON_COLOR}
            {...props}
            style={props.style}/>
    );
};

export default icon;

Which works only for FontAwesome, how can I make it dynamic based on eg prop parameter so I can use Material icons when I need to?仅适用于 FontAwesome,如何根据例如 prop 参数使其动态化,以便在需要时使用 Material 图标? Note: I wouldn't like to create separate components for each type eg IconMaterial, IconFontAwesome etc. I want the name of the component to be Icon regardless of type.注意:我不想为每种类型创建单独的组件,例如 IconMaterial、IconFontAwesome 等。我希望组件的名称为 Icon 而不管类型如何。 Is that possible?那可能吗?

You could pass a prop called iconFamily to your icon component: 您可以将名为iconFamily的道具iconFamily给图标组件:

Inside your Icon Component you are importing all the Fonts you want to use, eg: 在图标组件内部,您要导入所有要使用的字体,例如:

import IconFA from "react-native-vector-icons/FontAwesome";
import IconMA from "react-native-vector-icons/Material";

Then inside Icon's render function: 然后在Icon的render函数中:

  const Icon = (props) => {

  renderIcon = () => {
    if (props.iconFamily == "FA") {
      return (
        <IconFA
        size={23}
        {...props}
        style={props.style}/>
      );
   } 
   if (props.iconFamily == "MA") {
      return (
        <IconMA
        size={23}
        {...props}
        style={props.style}/>
      );
   }
  }
  return (
      renderIcon()
  )
}

An when you are using your custom icon component you just have to specify the iconFamily prop: 当您使用自定义图标组件时,只需指定iconFamily道具即可:

 <Icon iconFamily="FA" name="home" color="green" /> 
 <Icon iconFamily="MA" name="code" color="red" />

Output: 输出:

产量

Complete Working Example: 完整的工作示例:

https://snack.expo.io/@tim1717/humiliated-hummus https://snack.expo.io/@tim1717/humiliated-hummus

I always liked doing it that way.我一直喜欢这样做。 ~/components/VectorIcons.js ~/components/VectorIcons.js

import AntDesign from 'react-native-vector-icons/AntDesign'
import Entypo from 'react-native-vector-icons/Entypo'
import EvilIcons from 'react-native-vector-icons/EvilIcons'
import Feather from 'react-native-vector-icons/Feather'
import FontAwesome from 'react-native-vector-icons/FontAwesome'
import FontAwesome5 from 'react-native-vector-icons/FontAwesome5'
import FontAwesome5Pro from 'react-native-vector-icons/FontAwesome5Pro'
import Fontisto from 'react-native-vector-icons/Fontisto'
import Foundation from 'react-native-vector-icons/Foundation'
import Ionicons from 'react-native-vector-icons/Ionicons'
import MaterialCommunityIcons from 'react-native-vector-icons/MaterialCommunityIcons'
import MaterialIcons from 'react-native-vector-icons/MaterialIcons'
import Octicons from 'react-native-vector-icons/Octicons'
import SimpleLineIcons from 'react-native-vector-icons/SimpleLineIcons'
import Zocial from 'react-native-vector-icons/Zocial'


const VectorIcon = {
  AntDesign,
  Entypo,
  EvilIcons,
  Feather,
  FontAwesome,
  FontAwesome5,
  FontAwesome5Pro,
  Fontisto,
  Foundation,
  Ionicons,
  MaterialCommunityIcons,
  MaterialIcons,
  Octicons,
  SimpleLineIcons,
  Zocial,
}

export default VectorIcon

To use in any jsx ~/pages/Home/index.jsx在任何 jsx 中使用 ~/pages/Home/index.jsx

import VectorIcon from '../../components/VectorIcon'

return (
  <>
   // ...
   <VectorIcon.AntDesign name="home" />
   <VectorIcon.Fontisto name="clock-outline" />
  </>
)

This is my Icon Component, and maybe you can get an idea from this.这是我的图标组件,也许你可以从中得到一个想法。

import React, { useEffect, useState } from "react";
import {
  Ionicons,
  AntDesign,
  Entypo,
  EvilIcons,
  Feather,
  FontAwesome,
  FontAwesome5,
  Fontisto,
  Foundation,
  MaterialCommunityIcons,
  MaterialIcons,
  Octicons,
  SimpleLineIcons,
  Zocial,
} from "@expo/vector-icons";

const Icon= ({ family, name, ...props }) => {
  const [icon, setIcon] = useState(null);

  useEffect(() => {
    let Family;
    switch (family) {
      case "AntDesign":
        Family = AntDesign;
        break;
      case "Entypo":
        Family = Entypo;
        break;
      case "EvilIcons":
        Family = EvilIcons;
        break;
      case "Feather":
        Family = Feather;
        break;
      case "FontAwesome":
        Family = FontAwesome;
        break;
      case "FontAwesome5":
        Family = FontAwesome5;
        break;
      case "Fontisto":
        Family = Fontisto;
        break;
      case "Foundation":
        Family = Foundation;
        break;
      case "Ionicons":
        Family = Ionicons;
        break;
      case "MaterialCommunityIcons":
        Family = MaterialCommunityIcons;
        break;
      case "MaterialIcons":
        Family = MaterialIcons;
        break;
      case "Octicons":
        Family = Octicons;
        break;
      case "SimpleLineIcons":
        Family = SimpleLineIcons;
        break;
      case "Zocial":
        Family = Zocial;
        break;
      default:
        Family = Ionicons;
    }

    if (Family) {
      setIcon(<Family name={name ? name : "help-outline"} color={"#000"} size={20} {...props} />);
    }
  }, [family, name]);

  return icon;
};

export default Icon;

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

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