前言
版本:
“react-native”: “^0.72.5”,
“react-native-vector-icons”: “^10.0.0”
按照React Native Elements教程安装react-native-vector-icons。
yarn add react-native-vector-icons
npx react-native link react-native-vector-icons
该指令npx react-native link react-native-vector-icons 报link未识别。说明当react-native 库已经没有了link指令。为了解决该问题,需要手动添加react-native-asset库。
安装react-native-asset
yarn add react-native-asset --save
根目录新建 react-native.config.js
module.exports = {
assets: ['node_modules/react-native-vector-icons']
};
手动link
yarn react-native-asset
使用react-native-vector-icons库里的Icon
完成上述步骤后,按照React Native Element教程使用Icon。但是教程比较模糊,这样讲更详细一些。
node_modules/@types/react-native-vector-icons文件夹里如下图:
可以看到里面有 AntDesign、FontAwesome、MaterialIcons等包。手动导入想要的包名。再在react-native-vector-icons directory网站里查找想要的图标。
import Icon from 'react-native-vector-icons/AntDesign';
function VideoItem(props: { video: VideoParam }): JSX.Element {
const { video } = props;
return (
<View style={styles.videoContainer}>
<ImageBackground source={{ uri: video.image }} resizeMode="cover" style={styles.image}>
<View>
<Icon
name='playcircleo' />
</View>
</ImageBackground>
</View>
)
}
后续:
发现上面手动link方法,会在第二次打包应用时会报错。如果出现该错误,推荐使用这篇攻略:
【React Native】react-native-vector-icons用法避坑