I am trying to use bootstrap icons in react native and I can’t find any useful methods on how to render an SVG in react-native. Does anyone know how to?
Advertisement
Answer
To Add SVGs in react native you can use react-native-svg
Install:
yarn add react-native-svg
Example:
JavaScript
x
65
65
1
import Svg, {
2
Circle,
3
Ellipse,
4
G,
5
Text,
6
TSpan,
7
TextPath,
8
Path,
9
Polygon,
10
Polyline,
11
Line,
12
Rect,
13
Use,
14
Image,
15
Symbol,
16
Defs,
17
LinearGradient,
18
RadialGradient,
19
Stop,
20
ClipPath,
21
Pattern,
22
Mask,
23
} from 'react-native-svg';
24
25
/* Use this if you are using Expo
26
import * as Svg from 'react-native-svg';
27
const { Circle, Rect } = Svg;
28
*/
29
30
import React from 'react';
31
import { View, StyleSheet } from 'react-native';
32
33
export default class SvgExample extends React.Component {
34
render() {
35
return (
36
<View
37
style={[
38
StyleSheet.absoluteFill,
39
{ alignItems: 'center', justifyContent: 'center' },
40
]}
41
>
42
<Svg height="50%" width="50%" viewBox="0 0 100 100">
43
<Circle
44
cx="50"
45
cy="50"
46
r="45"
47
stroke="blue"
48
strokeWidth="2.5"
49
fill="green"
50
/>
51
<Rect
52
x="15"
53
y="15"
54
width="70"
55
height="70"
56
stroke="red"
57
strokeWidth="2"
58
fill="yellow"
59
/>
60
</Svg>
61
</View>
62
);
63
}
64
}
65
Or you want to import an svg to your app use: react-native-svg-transformer
Example:
JavaScript
1
4
1
import Logo from "./logo.svg";
2
3
<Logo width={120} height={40} />
4
But if you want an icon library you can use : react-native-vector-icons
Also here is the directory: React Native Vector Icons directory