[英]Can't render Svg icons in custom React Native Component
我在渲染通过自定义组件中的道具提供的图标时遇到问题。 这是我的自定义组件:
import React from 'react';
import { View, Image, StyleSheet, Text } from 'react-native';
export class MoreIcon extends React.Component {
render() {
return(
<View style={styles.topViewStyle}>
<View style={styles.circleStyle}>
<Image source={this.props.iconSource} style={styles.iconStyle} />
</View>
<Text style={styles.moreIconText}>{this.props.iconText}</Text>
</View>
);
}
}
const styles = StyleSheet.create({
topViewStyle: {
alignItems: 'center',
justifyContent: 'center',
},
moreIconText: {
fontSize: 12,
fontWeight: 'bold',
color: '#fff',
lineHeight: 1.17,
height: 14,
},
iconStyle: {
width: 30,
height: 30,
},
circleStyle: {
width: 58,
height: 58,
borderRadius: 50,
borderColor: 'rgba(255, 255, 255, 0.3)',
borderWidth: 2,
}
});
问题是,当我在这样的组件中提供source属性时:
<MoreIcon iconText='Home' iconSource={require('../../assets/icons/dashboard.svg')} />
我所提供的道具未显示在MoreIcon
选项卡中的地方,我在做什么错? (路径正确)
您需要将SVG转换为与react-native-svg库兼容。 幸运的是,自述文件的“ 用法”部分有一些很好的示例。
转换它们之后,就可以import
它们作为普通常数import
。
编辑:
根据下面亚当的评论,使用react-native-svg-uri
库现在似乎是解决SVG问题的更简单方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.