React 本机矢量图标-动态类型
React native vector icons - dynamic type
我使用不同类型的 React 本机矢量图标 - Material 和 FontAwesome,具体取决于特定图标的可用性。我想创建一个通用组件来包装整个应用程序中图标的使用。到目前为止我有:
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;
它只适用于 FontAwesome,我怎样才能使它动态基于例如prop 参数以便我可以在需要时使用 Material 图标?注意:我不想为每种类型创建单独的组件,例如IconMaterial, IconFontAwesome 等等 我希望组件的名称是 Icon 而不管类型。这可能吗?
您可以将名为 iconFamily
的道具传递给您的图标组件:
在您的图标组件中,您正在导入您要使用的所有字体,例如:
import IconFA from "react-native-vector-icons/FontAwesome";
import IconMA from "react-native-vector-icons/Material";
然后在 Icon 的渲染函数中:
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()
)
}
当您使用自定义图标组件时,您只需指定 iconFamily 属性:
<Icon iconFamily="FA" name="home" color="green" />
<Icon iconFamily="MA" name="code" color="red" />
输出:
完整的工作示例:
我一直喜欢这样做。
~/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
在任何jsx中使用
~/pages/Home/index.jsx
import VectorIcon from '../../components/VectorIcon'
return (
<>
// ...
<VectorIcon.AntDesign name="home" />
<VectorIcon.Fontisto name="clock-outline" />
</>
)
这是我的图标组件,也许你可以从中得到灵感。
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 }) => {
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;
}
return (
<Family
name={name ? name : "help-outline"}
color={"#000"}
size={20}
{...props}
/>
);
};
export default Icon;
要使用它,您可以这样做。
<Icon
family="Ionicons"
name="add-outline"
color={"#333"}
size={30}
/>
我添加了默认的家庭和名字以避免错误。
我使用不同类型的 React 本机矢量图标 - Material 和 FontAwesome,具体取决于特定图标的可用性。我想创建一个通用组件来包装整个应用程序中图标的使用。到目前为止我有:
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;
它只适用于 FontAwesome,我怎样才能使它动态基于例如prop 参数以便我可以在需要时使用 Material 图标?注意:我不想为每种类型创建单独的组件,例如IconMaterial, IconFontAwesome 等等 我希望组件的名称是 Icon 而不管类型。这可能吗?
您可以将名为 iconFamily
的道具传递给您的图标组件:
在您的图标组件中,您正在导入您要使用的所有字体,例如:
import IconFA from "react-native-vector-icons/FontAwesome";
import IconMA from "react-native-vector-icons/Material";
然后在 Icon 的渲染函数中:
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()
)
}
当您使用自定义图标组件时,您只需指定 iconFamily 属性:
<Icon iconFamily="FA" name="home" color="green" />
<Icon iconFamily="MA" name="code" color="red" />
输出:
完整的工作示例:
我一直喜欢这样做。 ~/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
在任何jsx中使用 ~/pages/Home/index.jsx
import VectorIcon from '../../components/VectorIcon'
return (
<>
// ...
<VectorIcon.AntDesign name="home" />
<VectorIcon.Fontisto name="clock-outline" />
</>
)
这是我的图标组件,也许你可以从中得到灵感。
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 }) => {
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;
}
return (
<Family
name={name ? name : "help-outline"}
color={"#000"}
size={20}
{...props}
/>
);
};
export default Icon;
要使用它,您可以这样做。
<Icon
family="Ionicons"
name="add-outline"
color={"#333"}
size={30}
/>
我添加了默认的家庭和名字以避免错误。